Skip to contentSkip to conversation

Variable Quoting

DISCUSSION Paul Kraus 4 participants 8 messages Oct 14, 2002 – Oct 16, 2002 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Paul Kraus

author
#1 lore raw

That is what I did.

---Directory is real and does exist and path verified using file completion.
Here is an example.
string="/My Documents"
ls "$string"

Does work as it is intended.

Now with this change
string="My Documents/*"
cp "$string" /tmp

cp: cannot stat 'My Documents/*':no such file or directory So then I for kicks I tried.

cp "My Documents/*" /tmp
Produces the same error.

So just to make sure that it was not a typo in path I tried. cp My\ Documents/* /tmp

And this worked.

So is the problem with cp? Since the same syntax seems to work with all other apps?
Is cp not capable of taking a double quoted path with spaces?

Redhat 7.3 in case it helps.

-----Original Message-----
From: James Mohr [mailto:[email protected]]
Sent: Monday, October 14, 2002 2:55 PM
To: Paul Kraus
Subject: Re: Variable Quoting

Quoted 11 lines · Paul Kraus

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

Quoted 2 lines

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

Quoted 4 lines

I also get an error.

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

Hi Paul!
If you assign a value to a variable using quotes, then the value is assigned
** without** the quotes. This is because the shell gets ahold of it a reduces
each command to "tokens" and one of the tokens is the text **inside** the
quotes. Also the expansion takes place by the shell before it is passed on to
the copy command.

Look at these examples:
mdkir 'My Documents'
DIR='My Documents'
ls $DIR
ls: My: No such file or directory
ls: Documents: No such file or directory

Makes sense as the variable $DIR contains the literal text, including the
space. The command that is execute is:

ls My Documents

and neither of these exists. If you try:

ls -ld "$DIR"
drwxr-xr-x 2 root root 48 Oct 14 20:44 My Documents

This works because the $DIR is expanded by the shell and it plus the quotes
become the "token" which is then passed to the the ls command.

Your examples will work as long as you ensure that the variables are include
inside of quotes, for example, this works:

cp "$somevarable" "$somedestination"

Regards,

jimmo

Signature · 16 lines
---------------------------------------
"Be more concerned with your character than with your reputation. Your
character is what you really are while your reputation is merely what
others think you are." -- John Wooden
---------------------------------------
Be sure to visit the Linux Tutorial:  http://www.linux-tutorial.info
---------------------------------------
NOTE: All messages sent to me in response to my posts to newsgroups or
forums 
are subject to reposting.

-
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

On Monday 14 October 2002 20:38, Paul Kraus wrote:
<SNIP>

Quoted 5 lines

Now with this change
string="My Documents/*"
cp "$string" /tmp

cp: cannot stat 'My Documents/*':no such file or directory

Sure because the token that is passed to the cp command includes the asterisk, it doesn't know that it should expand the asterisk (*) to the file names, so it looks for a file with the literal name '*'.

Quoted 4 lines

So then I for kicks I tried.

cp "My Documents/*" /tmp
Produces the same error.

Same thing.

Quoted 5 lines

So just to make sure that it was not a typo in path I tried. cp My\ Documents/* /tmp

And this worked.

Makes sense as the shell is now expanding the asterisks to the names of the files. You don't see this because it is expanded internally. Create a shell script that just does this copy and put "set -x" on the first list. This should show you the expanded form before it is passed to cp.

Quoted 3 lines

So is the problem with cp? Since the same syntax seems to work with all other apps?
Is cp not capable of taking a double quoted path with spaces?

No, it is the fact that cp does not know to expand the asterisk. Plus you are passing different things to the cp depending on what sets of quotes you use.

Quoted 1 line

Redhat 7.3 in case it helps.

Actually the Linux distriubtion is pretty irrelevant in this case. In fact the behavior would be the same on most any *NIX system, as it is standard shell stuff.

See if these explain more of the details for you:

http://www.linux-tutorial.info/cgi-bin/display.pl?20&0&0&0&3
http://www.linux-tutorial.info/cgi-bin/display.pl?22&0&0&0&3

regards,

jimmo

Signature · 14 lines
---------------------------------------
"Be more concerned with your character than with your reputation. Your
character is what you really are while your reputation is merely what others
think you are." -- John Wooden
---------------------------------------
Be sure to visit the Linux Tutorial:  http://www.linux-tutorial.info
---------------------------------------
NOTE: All messages sent to me in response to my posts to newsgroups or forums 
are subject to reposting.
-
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 lied. I understand why its not working but how should I workaround it?
Do I have to have my script gather a list of the files and the pass it the variable?
That sucks that I can not use a wildcard to just get all.

I have made these changes. Which I though would fix my problem. string="My Documents/"
cp "${string}*" Which I thought would eliminate the problem. I guess I am not understanding.

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of James Mohr
Sent: Monday, October 14, 2002 3:58 PM
To: [email protected]
Subject: Re: Variable Quoting

On Monday 14 October 2002 20:38, Paul Kraus wrote:
<SNIP>

Quoted 5 lines

Now with this change
string="My Documents/*"
cp "$string" /tmp

cp: cannot stat 'My Documents/*':no such file or directory

Sure because the token that is passed to the cp command includes the asterisk,
it doesn't know that it should expand the asterisk (*) to the file names, so
it looks for a file with the literal name '*'.

Quoted 4 lines

So then I for kicks I tried.

cp "My Documents/*" /tmp
Produces the same error.

Same thing.

Quoted 5 lines

So just to make sure that it was not a typo in path I tried. cp My\
Documents/* /tmp

And this worked.

Makes sense as the shell is now expanding the asterisks to the names of the
files. You don't see this because it is expanded internally. Create a shell
script that just does this copy and put "set -x" on the first list. This

should show you the expanded form before it is passed to cp.

Quoted 3 lines

So is the problem with cp? Since the same syntax seems to work with all other apps? Is cp not capable of taking a double quoted path with spaces?

No, it is the fact that cp does not know to expand the asterisk. Plus you are
passing different things to the cp depending on what sets of quotes you use.

Quoted 1 line

Redhat 7.3 in case it helps.

Actually the Linux distriubtion is pretty irrelevant in this case. In fact the
behavior would be the same on most any *NIX system, as it is standard shell
stuff.

See if these explain more of the details for you:

http://www.linux-tutorial.info/cgi-bin/display.pl?20&0&0&0&3
http://www.linux-tutorial.info/cgi-bin/display.pl?22&0&0&0&3

regards,

jimmo

Signature · 21 lines
---------------------------------------
"Be more concerned with your character than with your reputation. Your
character is what you really are while your reputation is merely what
others think you are." -- John Wooden
---------------------------------------
Be sure to visit the Linux Tutorial:  http://www.linux-tutorial.info
---------------------------------------
NOTE: All messages sent to me in response to my posts to newsgroups or
forums 
are subject to reposting.
-
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

There are a couple of approaches.

One way is to enclose the entire path in quotes then stick the asterisk on the end - as in:

cp "My Documents/"* /tmp

which you can then use a variable for like this:

Path="My Documents"
cp "${Path}"/* /tmp

You could also use a for loop:

Path="My Documents/"

for FILENAME in ls "${Path}"
do

	cp "${Path}${Filename}" "/tmp"

done

Note that this second way of doing it will create "/tmp/My Documents"... but the first way will put the files in /tmp

P.

Quoted 104 lines · Paul Kraus

Ok I lied. I understand why its not working but how should I workaround it?
Do I have to have my script gather a list of the files and the pass it the variable?
That sucks that I can not use a wildcard to just get all.

I have made these changes. Which I though would fix my problem. string="My Documents/"
cp "${string}*" Which I thought would eliminate the problem. I guess I am not understanding.

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of James Mohr
Sent: Monday, October 14, 2002 3:58 PM
To: [email protected]
Subject: Re: Variable Quoting

On Monday 14 October 2002 20:38, Paul Kraus wrote:
<SNIP>

Quoted 5 lines

Now with this change
string="My Documents/*"
cp "$string" /tmp

cp: cannot stat 'My Documents/*':no such file or directory

Sure because the token that is passed to the cp command includes the asterisk,
it doesn't know that it should expand the asterisk (*) to the file names, so
it looks for a file with the literal name '*'.

Quoted 4 lines

So then I for kicks I tried.

cp "My Documents/*" /tmp
Produces the same error.

Same thing.

Quoted 5 lines

So just to make sure that it was not a typo in path I tried. cp My\
Documents/* /tmp

And this worked.

Makes sense as the shell is now expanding the asterisks to the names of the
files. You don't see this because it is expanded internally. Create a shell
script that just does this copy and put "set -x" on the first list. This

should show you the expanded form before it is passed to cp.

Quoted 3 lines

So is the problem with cp? Since the same syntax seems to work with all other apps? Is cp not capable of taking a double quoted path with spaces?

No, it is the fact that cp does not know to expand the asterisk. Plus you are
passing different things to the cp depending on what sets of quotes you use.

Quoted 1 line

Redhat 7.3 in case it helps.

Actually the Linux distriubtion is pretty irrelevant in this case. In fact the
behavior would be the same on most any *NIX system, as it is standard shell
stuff.

See if these explain more of the details for you:

http://www.linux-tutorial.info/cgi-bin/display.pl?20&0&0&0&3
http://www.linux-tutorial.info/cgi-bin/display.pl?22&0&0&0&3

regards,

jimmo

Signature · 21 lines
---------------------------------------
"Be more concerned with your character than with your reputation. Your
character is what you really are while your reputation is merely what
others think you are." -- John Wooden
---------------------------------------
Be sure to visit the Linux Tutorial:  http://www.linux-tutorial.info
---------------------------------------
NOTE: All messages sent to me in response to my posts to newsgroups or
forums 
are subject to reposting.
-
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
Signature · 11 lines
Paul Furness

Systems Manager

2+2=5 for extremely large values of 2.

-
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
#5 lore raw
Quoted 6 lines · Paul Furness

Path="My Documents/"
for FILENAME in ls "${Path}"
do

     cp "${Path}${Filename}" "/tmp"

done

I think you mean:
----------------------------------
Path="My Documents/"
for FILENAME in ls "${Path}"
do

    cp "${Path}${FILENAME}" "/tmp"

done
----------------------------------
Note the changes in lines two and four.

Cheers,
Steven

______________________________
http://www.volny.cz/basiclinux

-
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

#6 lore raw

Oops. <looks sheepish> I did indeed mean for both instances of the word "Filename" to be in the same case.
<sigh> I must be getting old; either that or I'm not drinking enough coke.

Apologies if I caused any confusion. :)

P.

Quoted 31 lines · On Wed, 2002-10-16 at 08:23, [email protected] wrote:
Quoted 6 lines · Paul Furness

Path="My Documents/"
for FILENAME in ls "${Path}"
do

     cp "${Path}${Filename}" "/tmp"

done

I think you mean:
----------------------------------
Path="My Documents/"
for FILENAME in ls "${Path}"
do

    cp "${Path}${FILENAME}" "/tmp"

done
----------------------------------
Note the changes in lines two and four.

Cheers,
Steven

______________________________
http://www.volny.cz/basiclinux

-
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

Signature · 11 lines
Paul Furness

Systems Manager

2+2=5 for extremely large values of 2.

-
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
#7 lore raw
In reply to James Mohr

GOT IT!!!

Cp "$variable"*

Thanks again!

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of James Mohr
Sent: Monday, October 14, 2002 3:58 PM
To: [email protected]
Subject: Re: Variable Quoting

On Monday 14 October 2002 20:38, Paul Kraus wrote:
<SNIP>

Quoted 5 lines

Now with this change
string="My Documents/*"
cp "$string" /tmp

cp: cannot stat 'My Documents/*':no such file or directory

Sure because the token that is passed to the cp command includes the asterisk,
it doesn't know that it should expand the asterisk (*) to the file names, so
it looks for a file with the literal name '*'.

Quoted 4 lines

So then I for kicks I tried.

cp "My Documents/*" /tmp
Produces the same error.

Same thing.

Quoted 5 lines

So just to make sure that it was not a typo in path I tried. cp My\
Documents/* /tmp

And this worked.

Makes sense as the shell is now expanding the asterisks to the names of the
files. You don't see this because it is expanded internally. Create a shell
script that just does this copy and put "set -x" on the first list. This

should show you the expanded form before it is passed to cp.

Quoted 3 lines

So is the problem with cp? Since the same syntax seems to work with all other apps? Is cp not capable of taking a double quoted path with spaces?

No, it is the fact that cp does not know to expand the asterisk. Plus you are
passing different things to the cp depending on what sets of quotes you use.

Quoted 1 line

Redhat 7.3 in case it helps.

Actually the Linux distriubtion is pretty irrelevant in this case. In fact the
behavior would be the same on most any *NIX system, as it is standard shell
stuff.

See if these explain more of the details for you:

http://www.linux-tutorial.info/cgi-bin/display.pl?20&0&0&0&3
http://www.linux-tutorial.info/cgi-bin/display.pl?22&0&0&0&3

regards,

jimmo

Signature · 21 lines
---------------------------------------
"Be more concerned with your character than with your reputation. Your
character is what you really are while your reputation is merely what
others think you are." -- John Wooden
---------------------------------------
Be sure to visit the Linux Tutorial:  http://www.linux-tutorial.info
---------------------------------------
NOTE: All messages sent to me in response to my posts to newsgroups or
forums 
are subject to reposting.
-
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
#8 lore raw
Quoted 3 lines · Paul Kraus

GOT IT!!!

Cp "$variable"*

Sort of. That obviously works. Since the asterisks and the variable are expanded seperately in this case, it works. However, it may not work in every case depending on what you are trying to accomplished. If you want to copy everything, including all sub-directories, you can forget the asterisk and simply use the -R option to cp.

Regards,

jimmo

Quoted 72 lines

Thanks again!

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of James Mohr
Sent: Monday, October 14, 2002 3:58 PM
To: [email protected]
Subject: Re: Variable Quoting

On Monday 14 October 2002 20:38, Paul Kraus wrote:
<SNIP>

Quoted 5 lines

Now with this change
string="My Documents/*"
cp "$string" /tmp

cp: cannot stat 'My Documents/*':no such file or directory

Sure because the token that is passed to the cp command includes the asterisk,
it doesn't know that it should expand the asterisk (*) to the file names, so
it looks for a file with the literal name '*'.

Quoted 4 lines

So then I for kicks I tried.

cp "My Documents/*" /tmp
Produces the same error.

Same thing.

Quoted 4 lines

So just to make sure that it was not a typo in path I tried. cp My\
Documents/* /tmp

And this worked.

Makes sense as the shell is now expanding the asterisks to the names of the
files. You don't see this because it is expanded internally. Create a shell
script that just does this copy and put "set -x" on the first list. This

should show you the expanded form before it is passed to cp.

Quoted 3 lines

So is the problem with cp? Since the same syntax seems to work with all other apps? Is cp not capable of taking a double quoted path with spaces?

No, it is the fact that cp does not know to expand the asterisk. Plus you are
passing different things to the cp depending on what sets of quotes you use.

Quoted 1 line

Redhat 7.3 in case it helps.

Actually the Linux distriubtion is pretty irrelevant in this case. In fact the
behavior would be the same on most any *NIX system, as it is standard shell
stuff.

See if these explain more of the details for you:

http://www.linux-tutorial.info/cgi-bin/display.pl?20&0&0&0&3
http://www.linux-tutorial.info/cgi-bin/display.pl?22&0&0&0&3

regards,

jimmo

Signature · 14 lines
---------------------------------------
"Be more concerned with your character than with your reputation. Your
character is what you really are while your reputation is merely what others
think you are." -- John Wooden
---------------------------------------
Be sure to visit the Linux Tutorial:  http://www.linux-tutorial.info
---------------------------------------
NOTE: All messages sent to me in response to my posts to newsgroups or forums 
are subject to reposting.
-
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