Skip to contentSkip to conversation

Variable Quoting

DISCUSSION Paul Kraus 3 participants 5 messages Oct 14, 2002

lore
Share & tools
lore

Paul Kraus

author
#0 lore raw

I am having trouble quoting with variables.

For example

source="/backup/My\ Documents/*"
cp ${source} /tmp

When I run this it will see everything up to my and bomb. Now if I type the exact same path in the exact same way into the code

cp /backup/My\ Documents/*"
It will work fine.

This is not a real world example. I made this up for this email. I am having this kind of problems with many commands.
Another better example would be...

subj="This is a test email"
mutt -s ${subj} someemailaddress < somemessage

When this is run This becomes the subject and the rest of $subj became email address. I have also tried escaping the spaces with \ and it does not work. But when entered directly into the command it works fine.

Paul Kraus
Network Administrator
PEL Supply Company
216.267.5775 Voice
216-267-6176 Fax
www.pelsupply.com

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

#1 lore raw
Quoted 6 lines · Paul Kraus

I am having trouble quoting with variables.

For example

source="/backup/My\ Documents/*"
cp ${source} /tmp

either protect with quotes -or- escape the space...

source="/backup/My Documents/*" (quotes, no escape)
or
source=/backup/My\ Documents/* (escape, no quotes)

don't do both.

Quoted 7 lines

When I run this it will see everything up to my and bomb. Now if I type the exact same path in the exact same way into the code

cp /backup/My\ Documents/*"
It will work fine.

Quoted 6 lines

This is not a real world example. I made this up for this email. I am having this kind of problems with many commands.
Another better example would be...

subj="This is a test email"
mutt -s ${subj} someemailaddress < somemessage

protect the variable with quotes:

mutt -s "$subj" someemail.......

( by the way....

you don't have to surround the variable name with braces unless it's right up against something else, as in

filename="test"
cp somefile ${test}.txt

if it's seperated by white space they don't have to be there

)

Quoted 6 lines

When this is run This becomes the subject and the rest of $subj became email address. I have also tried escaping the spaces with \ and it does not work. But when entered directly into the command it works fine.

Quoted 6 lines

Paul Kraus
Network Administrator
PEL Supply Company
216.267.5775 Voice
216-267-6176 Fax
www.pelsupply.com

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

#2 lore raw
Quoted 11 lines

( by the way....

you don't have to surround the variable name with braces unless it's right up against something else, as in

filename="test"
cp somefile ${test}.txt

if it's seperated by white space they don't have to be there

)

oops - my face is red now.....

that should have been

filename="test"
cp somefile $(filename).txt

sorry
-jdr-

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

author
#3 lore raw

Ok I found out how to prevent it for separating the words as different arguments. I need to encapsulate the variable with quotes.

How ever I am needing to do this with the cp command. When I try double quoting a path with spaces in it for cp I get an error. If I make the string somevariable="somediretory/somesubdirectory/some\ directory\
with\ spaces/*"
Then when substition takes place
cp $somevarable $somedestination
Then it should read
cp /somedirectory/somesubdirectory/some\ directory\ with\ spaces/* /tmp This is correct syntax for cp and if I type it out or use file name completion that is the format the shell uses. However when done this way I also get an error.

It appears that substition is taking place but it is handling the escape sequence wrong.

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Jim Reimer
Sent: Monday, October 14, 2002 12:39 PM
To: unlisted-recipients:; no To-header on input
Cc: Paul Kraus; [email protected]
Subject: Re: Variable Quoting

Quoted 11 lines

( by the way....

you don't have to surround the variable name with braces unless it's right up against something else, as in

filename="test"
cp somefile ${test}.txt

if it's seperated by white space they don't have to be there

)

oops - my face is red now.....

that should have been

filename="test"
cp somefile $(filename).txt

sorry
-jdr-

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

#4 lore raw
In reply to Paul Kraus

Give this a try

Quoted 1 line

string somevariable="somediretory/somesubdirectory/some\ directory\

with\ spaces/*"

string somevariable="somediretory/somesubdirectory/some directory with spaces/*"

Quoted 1 line

cp $somevarable $somedestination

cp "$somevarable" $somedestination

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs