Search code examples
environment-variableswhitespacecd

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


Solution

  • 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