如何在Bash变量中添加带空格的路径 [英] How to add path with space in Bash variable
问题描述
如何在.bashrc
的Bash变量中添加带空格的路径?我想在.bashrc
中为路径存储一些变量,但是遇到了一个路径中带有空格的
How can I add a path with a space in a Bash variable in .bashrc
? I want to store some variables in .bashrc
for paths and I encountered a path with a space in it.
我尝试将其添加到' '
之间或使用转义字符\
,但没有帮助:
I tried to add it between ' '
or use the escape character \
, but it didn't help:
games=/run/media/mohamedRadwan/games\ moves # this doesn't work
games='/run/media/mohamedRadwan/games moves' # or this
games="/run/media/mohamedRadwan/games moves" # or this
...当我跑步时:
mount $games
...它抛出一个错误,表明它只是试图挂载/run/media/mohamedRadwan/games
.
... it throws an error indicating that it's only trying to mount /run/media/mohamedRadwan/games
.
但是当我运行echo $games
时,它会显示完整值/run/media/mohamedRadwan/games moves
.
But when I run echo $games
, it shows the full value, /run/media/mohamedRadwan/games moves
.
我该如何解决?
推荐答案
mount /dev/sda9 "$games"
如前所述,请始终引用变量取消引用.否则,外壳程序会将变量值中的空格混淆为分隔多个值的空格.
As mentioned, always quote variable dereferences. Otherwise, the shell confuses the spaces in the variable's value as spaces separating multiple values.
这篇关于如何在Bash变量中添加带空格的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!