How to store a path with white spaces into a variable in bash

后端 未结 2 1040
星月不相逢
星月不相逢 2021-01-20 15:55

I want to store /c/users/me/dir name into a variable to pass it to cd system call.

Works when typing:

$ cd \'/c/users/me/dir

2条回答
  •  礼貌的吻别
    2021-01-20 16:30

    While using a bash variable you should double-quote it to preserve its state.

    x='/home/ps/temp/bla bla'
     cd $x      ### <----used without double quotes. 
    sh: cd: /home/ps/temp/bla: No such file or directory
    
    
     cd "$x"    ### <---While using a bash variable you should double-quote it to presever its state.
     pwd
    /home/ps/temp/bla bla
    

提交回复
热议问题