如何在bash别名中转义空格? [英] How to escape whitespace in a bash alias?
问题描述
试图在我的.bashrc文件中设置一些别名.这一个...
导出别名umusic ="/卷/180克/未压缩/"
...出现以下错误...
-bash:cd:/Volumes/180:没有这样的文件或目录
...当我尝试"cd $ umusic"时.
我尝试了各种方法来将目录名中的空格转义,但无济于事.(180克,180%20克,单引号,双引号,无引号.)我知道最简单的解决方案是将目录重命名为"180gram",但是我想知道如何解决此特定问题.>
我在Mac上,如果有什么区别.
您对 export
命令的使用使 umusic
成为环境变量而不是别名. export
命令导出在命令行其余部分命名的环境变量,并可选地使用新值.因此,它将导出一个名为 alias
的环境变量(可能未设置)和一个名为 umusic
的环境变量.
鉴于您要扩展环境变量,shell将执行以下替换:
cd $ umusiccd/卷/180克/未压缩/
这会产生错误,因为没有加空格.相反,如果您这样做:
cd"$ umusic"
然后扩展为
cd"/卷/180克/未压缩/"
这就是您所期望的.
但是,由于必须引用扩展,为此使用环境变量可能仍然需要太多工作.相反,请尝试以下别名:
alias umusic ="cd'/Volumes/180 gram/Uncompressed'"
只需运行即可
$ umusic$ pwd/份/180克/未压缩
Tried to set some aliases in my .bashrc file. This one...
export alias umusic="/Volumes/180 gram/Uncompressed/"
...gets the following error...
-bash: cd: /Volumes/180: No such file or directory
...when I try "cd $umusic".
I've tried various methods of escaping that whitespace in the directory name, to no avail. (180\ gram, 180%20gram, single quotes, double quotes, no quotes.) I realize the easiest solution is to rename the directory to "180gram," but I'd like to know how to solve this particular problem.
I'm on a Mac, if that makes any difference.
Your use of the export
command is making umusic
an environment variable and not an alias. The export
command exports environment variables named on the rest of the command line, optionally with new values. So it's exporting an environment variable named alias
(which probably isn't set) and one named umusic
.
Given that you're expanding an environment variable, the shell does the following substitution:
cd $umusic
cd /Volumes/180 gram/Uncompressed/
which generates the error you get because the space isn't quoted. If instead you do:
cd "$umusic"
then the expansion is
cd "/Volumes/180 gram/Uncompressed/"
which is what you're expecting.
However, using an environment variable for this might be still a bit too much work, since you have to quote the expansion. Instead, try this alias:
alias umusic="cd '/Volumes/180 gram/Uncompressed'"
which you would run with just
$ umusic
$ pwd
/Volumes/180 gram/Uncompressed
这篇关于如何在bash别名中转义空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!