我有一个shell脚本,提示用户输入路径:
read -e -p "Enter the path to the file: " FILEPATH
eval FILEPATH=$FILEPATH
如果我这样输入一个:
Enter the path to the file: ~/Desktop/Hello\ My\ Name\ is\ Oliver/
然后我得到以下输出(在调试模式下bash):
Enter the path to the file: ~/Desktop/Hello\ My\ Name\ is\ Oliver/
+ eval 'FILEPATH=~/Desktop/Hello My Name is Oliver/'
++ FILEPATH=/Users/Oliver/Desktop/Hello
++ My Name is Oliver/
当路径中有特殊字符时,我也有类似的问题。
我该怎么做才能解决这个问题?
修改: eval
将~
之类的别名更正为正确的路径。
答案 0 :(得分:2)
如果你只需要展开{{1}},那么做一些像
这样的事情会更安全~
这也将变量保持在一起。