Bash script to cd to directory with spaces in pathname

后端 未结 13 1755
旧时难觅i
旧时难觅i 2020-11-28 21:14

I\'m using Bash on macOS X and I\'d like to create a simple executable script file that would change to another directory when it\'s run. However, the path to that director

相关标签:
13条回答
  • 2020-11-28 21:30

    Use single quotes, like:

    myPath=~/'my dir'
    
    cd $myPath
    
    0 讨论(0)
  • 2020-11-28 21:31

    When working under Linux the syntax below is right:

    cd ~/My\ Code

    However when you're executing your file, use the syntax below:

    $ . cdcode

    (just '.' and not './')

    0 讨论(0)
  • 2020-11-28 21:34

    The very simple way of doing this is-

     $ cd My\ Folder
    

    In bash, run DIR command and in the results you would see that the folder or path names having space between them has been written in the results like this -

    $dir
    My\ Folder
    New\ Folder
    
    0 讨论(0)
  • 2020-11-28 21:34

    I had a similar problem now were I was using a bash script to dump some data. I ended up creating a symbolic link in the script folder with out any spaces in it. I then pointed my script to the symbolic link and that works fine.

    To create your link. ln -s [TARGET DIRECTORY OR FILE] ./[SHORTCUT]

    Mau or may not be of use.

    0 讨论(0)
  • 2020-11-28 21:35

    Avoid ~ in scripts; use $HOME instead.

    0 讨论(0)
  • 2020-11-28 21:36
    cd ~/My\ Code
    

    seems to work for me... If dropping the quotes but keeping the slash doesn't work, can you post some sample code?

    0 讨论(0)
提交回复
热议问题