如何在 Bash 变量中添加带空格的路径 [英] How to add path with space in Bash variable

查看:37
本文介绍了如何在 Bash 变量中添加带空格的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 .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 move.

But when I run echo $games, it shows the full value, /run/media/mohamedRadwan/games moves.

我该如何解决这个问题?

How can I solve this?

推荐答案

mount /dev/sda9 "$games"

如前所述,始终引用变量解引用.否则,shell 会将变量值中的空格混淆为分隔多个值的空格.

As mentioned, always quote variable dereferences. Otherwise, the shell confuses the spaces in the variable's value as spaces separating multiple values.

这篇关于如何在 Bash 变量中添加带空格的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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