ralph ralph - 7 months ago 22
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

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.