0

I can't get path from variable in bash. How do it correct? For example:

my@PC:~$ a="~/.bashrc"
my@PC:~$ cat $a
cat: ~/.bashrc: No such file or directory

didn't work, but

cat .bashrc

and

cat ".bashrc"

Works well.


Here is right answer from fedorqui

cat $(eval echo $a)
4

1 回答 1

3

问题的原因是波浪号被 shell 扩展到主目录。当您将其存储在变量中时,波浪号不会展开,并且 cat 在文件夹 ~ (而不是您的主目录)中查找文件 .bashrc

有两种方法可以解决这个问题:建议的 eval 和使用 $HOME:

a="$HOME/.bashrc"
于 2013-07-25T16:05:05.160 回答