Tomas.R
Tomas.R

Reputation: 963

environment variable with cd to pathname with spaces : no such file or directory

The pathname with space is in quotes but the environment variable doesn't want to work, how come ?

enter image description here

Upvotes: 2

Views: 438

Answers (2)

Neu Romancer
Neu Romancer

Reputation: 1

This issue is very easy to work around: (Note I do this in a bash script, but doing it in any shell should work)

 oldIFS=$IFS 
    # save the original delims
 IFS="" 
    # Now erase all the delims
 DIR="/Users/dr_xemacs/Library/Application Support/minecraft/assets/indexes"
    # Assigning DIR now takes the whole line without cutting it at any delim
 IFS=$oldIFS 
    # reset delims

...and we have success.

Upvotes: 0

Kurtis Rader
Kurtis Rader

Reputation: 7469

Your attempt doesn't work due to the awful variable expansion and word splitting rules of POSIX shells. The comment by @ZiggZagg wasn't a suggestion to replace the double-quotes you're using with single-quotes. They were suggesting you single-quote the path. However, that won't work because of how var expansion interacts with word splitting. Basically, there is no way to do what you're trying to do. You can, however, just put the path in the var and use that:

photos='/home/tomas/Pictures/New Folder'
cd "$photos"

Note the need to quote the var expansion to keep the shell from doing word splitting on the expansion.

You can get closer to what you want by using an alias or function. For example,

photos() { cd '/home/tomas/Pictures/New Folder'; }
photos

Upvotes: 3

Related Questions