路径中的Tilde不会扩展到主目录 [英] Tilde in path doesn't expand to home directory
本文介绍了路径中的Tilde不会扩展到主目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我在/home/user/
中有一个名为Foo
的文件夹(我的/home/user
也由~
表示).
Say I have a folder called Foo
located in /home/user/
(my /home/user
also being represented by ~
).
我想要一个变量
a="~/Foo"
然后执行
cd $a
我明白了
-bash: cd: ~/Foo: No such file or directory
但是,如果我只是执行cd ~/Foo
,它就可以正常工作.关于如何使它工作的任何线索?
However if I just do cd ~/Foo
it works fine. Any clue on how to get this to work?
推荐答案
您可以(在变量赋值期间不加引号):
You can do (without quotes during variable assignment):
a=~/Foo
cd "$a"
但是在这种情况下,变量$a
将不存储~/Foo
,而是存储扩展形式/home/user/Foo
.或者您可以使用eval
:
But in this case the variable $a
will not store ~/Foo
but the expanded form /home/user/Foo
. Or you could use eval
:
a="~/Foo"
eval cd "$a"
这篇关于路径中的Tilde不会扩展到主目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文