Bash script to cd to directory with spaces in pathname

后端 未结 13 1691
旧时难觅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:29

    When you double-quote a path, you're stopping the tilde expansion. So there are a few ways to do this:

    cd ~/"My Code"
    cd ~/'My Code'
    

    The tilde is not quoted here, so tilde expansion will still be run.

    cd "$HOME/My Code"
    

    You can expand environment variables inside double-quoted strings; this is basically what the tilde expansion is doing

    cd ~/My\ Code
    

    You can also escape special characters (such as space) with a backslash.

提交回复
热议问题