路径中的波浪号不会扩展到主目录 [英] Tilde in path doesn't expand to home directory

查看:21
本文介绍了路径中的波浪号不会扩展到主目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在 /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: 没有那个文件或目录

但是,如果我只执行 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"

这篇关于路径中的波浪号不会扩展到主目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆