ralph ralph - 2 years ago 205
Bash Question

bash environment variable with whitespace character. cd without quotes

I'm trying to succeed with the following in bash:

mkdir "any dir"
export DIR="any dir"
cd $DIR

and it gives me
-bash: cd: any: No such file or directory

I know it would work with

cd "$DIR"

but I do not want to use this because a ton of scripts I do not want to touch uses
cd $DIR

Is there really no way to achieve this with bash? Even the Windows prompt allows this by

mkdir "any dir"
set DIR=any^ dir
cd %DIR%

Answer Source

One possible kludge is to use [[:blank:]] instead of space like this:

export DIR='any[[:blank:]]dir'
cd $DIR

Now it will work without error since shell won't treat any[[:blank:]]dir as 2 different arguments.

[[:blank:]] is POSIX property that matches space or tab character.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download