variable expansion

I want to define a variable pointing to my desktop location, in bash I say

desktop="\"/cygdrive/c/Dokumente und Einstellungen/Luntain/Desktop\""

When I echo it looks ok, and yet when I input cd $desktop it complaints
that there is no /cygdrive/c/Dokumente directory. wtf? I also tried
defining desktop using '\ ', that is

desktop="/cygdrive/c/Dokumente\\ und\\ Ein...

I do it obviously in cygwin bash, and I don't know if it would work on
Linux.
Luntain [ Sa, 10 November 2007 13:15 ] [ ID #1867254 ]

Re: variable expansion

Luntain <news.20.luntain [at] spamgourmet.com> wrote:
> I want to define a variable pointing to my desktop location, in bash I say
>
> desktop="\"/cygdrive/c/Dokumente und Einstellungen/Luntain/Desktop\""
>
> When I echo it looks ok, and yet when I input cd $desktop it complaints

If the value of a variable contains whitespace, you must quote the
variable name when you reference it, if you want shell to treat it
as a single argument. Otherwise, shell interprets the spaces as
argument delimiters.

This means,

cd $desktop

is interpreted as a cd command with three arguments:

cd "/cygdrive/c/Dokumente" "und" "Einstellungen/Luntain/Desktop"

The cd command uses a single argument, so it will attempt to change
your working directory to /cygdrive/c/Dokumente, which does not exist.
If you surround the referenced variable name with double quotes,

cd "$desktop"

the shell will preserve the spaces and interpret this as a cd command
with a single argument

cd "/cygdrive/c/Dokumente und Einstellungen/Luntain/Desktop"

which is what you desire.

--
Kenan Kalajdzic
Kenan Kalajdzic [ Sa, 10 November 2007 14:25 ] [ ID #1867255 ]

Re: variable expansion

>> I want to define a variable pointing to my desktop location, in bash I say
>>
>> desktop="\"/cygdrive/c/Dokumente und Einstellungen/Luntain/Desktop\""
>>
>> When I echo it looks ok, and yet when I input cd $desktop it complaints
>
> If the value of a variable contains whitespace, you must quote the
> variable name when you reference it, if you want shell to treat it
> as a single argument. Otherwise, shell interprets the spaces as
> argument delimiters.
>
> This means,
>
> cd $desktop
>
> is interpreted as a cd command with three arguments:
>
> cd "/cygdrive/c/Dokumente" "und" "Einstellungen/Luntain/Desktop"
>
> The cd command uses a single argument, so it will attempt to change
> your working directory to /cygdrive/c/Dokumente, which does not exist.
> If you surround the referenced variable name with double quotes,
>
> cd "$desktop"
>
> the shell will preserve the spaces and interpret this as a cd command
> with a single argument
>
> cd "/cygdrive/c/Dokumente und Einstellungen/Luntain/Desktop"
>
> which is what you desire.
>

I am not that lame not to now that spaces seperate arguments.

I would like to point out that I assign a quoted string to desktop variable:

desktop="\"/cygdrive/c/Dokumente und Einstellungen/Luntain/Desktop\""

when I echo $desktop, the path is displayes with the enclosing quotes. I
don't understand why cd doesn't seem to see the quotes. I want to have a
quick way of changing to my desktop directory, this is the hole point of
that desktop variable. There must be a way.
Luntain [ Sa, 10 November 2007 15:50 ] [ ID #1867258 ]

Re: variable expansion

On 2007-11-10, Luntain <news.20.luntain [at] spamgourmet.com> wrote:
>
> I am not that lame not to now that spaces seperate arguments.
>
> I would like to point out that I assign a quoted string to desktop variable:
>
> desktop="\"/cygdrive/c/Dokumente und Einstellungen/Luntain/Desktop\""
>
> when I echo $desktop, the path is displayes with the enclosing quotes. I
> don't understand why cd doesn't seem to see the quotes. I want to have a
> quick way of changing to my desktop directory, this is the hole point of
> that desktop variable. There must be a way.

cd "$desktop" is one way. Another way is to use the CDPATH variable.
Bill Marcum [ Sa, 10 November 2007 16:02 ] [ ID #1867259 ]

Re: variable expansion

On 2007-11-10, Luntain <news.20.luntain [at] spamgourmet.com> wrote:
>
> desktop="\"/cygdrive/c/Dokumente und Einstellungen/Luntain/Desktop\""
>
> when I echo $desktop, the path is displayes with the enclosing quotes.

This is the expected behaviour. You can have a directory with
a name like: abc"xyz
in that case, you can cd in this way:
cd abc\"xyz

try: desktop="bla bla bla with as many / as you want"

But the \ is the scape character (special meaning). If you want a \
without special meaning, use \\

> I
> don't understand why cd doesn't seem to see the quotes. I want to have a
> quick way of changing to my desktop directory, this is the hole point of
> that desktop variable. There must be a way.

try just:

cd

Best regards,
Claudio.

(I apologize my bad english)
Claudio [ Sa, 10 November 2007 17:17 ] [ ID #1867260 ]

Re: variable expansion

On 2007-11-10, Luntain wrote:
>
> I want to define a variable pointing to my desktop location, in bash I say
>
> desktop="\"/cygdrive/c/Dokumente und Einstellungen/Luntain/Desktop\""
>
> When I echo it looks ok, and yet when I input cd $desktop it complaints
> that there is no /cygdrive/c/Dokumente directory. wtf? I also tried
> defining desktop using '\ ', that is
>
> desktop="/cygdrive/c/Dokumente\\ und\\ Ein...
>
> I do it obviously in cygwin bash, and I don't know if it would work on
> Linux.

The quotes are not part of the path; remove them.

desktop="/cygdrive/c/Dokumente und Einstellungen/Luntain/Desktop"

When you use it, quote the variable, e.g.:

cd "$desktop"

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
cfajohnson [ Sa, 10 November 2007 18:18 ] [ ID #1867263 ]

Re: variable expansion

In article <AyjZi.45383$c_1.39330 [at] text.news.blueyonder.co.uk>,
Luntain <news.20.luntain [at] spamgourmet.com> wrote:

> I would like to point out that I assign a quoted string to desktop variable:
>
> desktop="\"/cygdrive/c/Dokumente und Einstellungen/Luntain/Desktop\""
>
> when I echo $desktop, the path is displayes with the enclosing quotes. I
> don't understand why cd doesn't seem to see the quotes. I want to have a
> quick way of changing to my desktop directory, this is the hole point of
> that desktop variable. There must be a way.

Quote processing is not performed on the result of variable expansion.
So it's looking for a filename that actually starts with doublequote.

--
Barry Margolin, barmar [at] alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
Barry Margolin [ Sa, 10 November 2007 18:49 ] [ ID #1867264 ]

Re: variable expansion

>> I am not that lame not to now that spaces seperate arguments.
>>
>> I would like to point out that I assign a quoted string to desktop variable:
>>
>> desktop="\"/cygdrive/c/Dokumente und Einstellungen/Luntain/Desktop\""
>>
>> when I echo $desktop, the path is displayes with the enclosing quotes. I
>> don't understand why cd doesn't seem to see the quotes. I want to have a
>> quick way of changing to my desktop directory, this is the hole point of
>> that desktop variable. There must be a way.
>
> cd "$desktop" is one way. Another way is to use the CDPATH variable.

CDPATH is what I need, thx.

CDPATH=".:/cygdrive/c/Dokumente und Einstellungen/Luntain"
Luntain [ Sa, 10 November 2007 21:18 ] [ ID #1867265 ]
Linux » comp.unix.shell » variable expansion

Vorheriges Thema: different sed behaviour on mac os x and linux
Nächstes Thema: merge two files line by line...