Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
549 views
in Technique[技术] by (71.8m points)

terminal - How to add path with space in Bash variable

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

... when I run:

mount $games

... it throws an error indicating that it's only trying to mount /run/media/mohamedRadwan/games.

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

How can I solve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...