variable expansion

variable expansion

am 10.11.2007 13:15:41 von Luntain

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.

Re: variable expansion

am 10.11.2007 14:25:31 von Kenan Kalajdzic

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

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

Re: variable expansion

am 10.11.2007 15:50:08 von Luntain

>> 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.

Re: variable expansion

am 10.11.2007 16:02:31 von Bill Marcum

On 2007-11-10, Luntain 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.

Re: variable expansion

am 10.11.2007 17:17:06 von Claudio

On 2007-11-10, Luntain 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)

Re: variable expansion

am 10.11.2007 18:18:34 von cfajohnson

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
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

Re: variable expansion

am 10.11.2007 18:49:06 von Barry Margolin

In article ,
Luntain 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@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 ***

Re: variable expansion

am 10.11.2007 21:18:58 von Luntain

>> 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"