cd: 参数太多 - 空格正确转义 + 引号?
cd: too many arguments - spaces properly escaped + quoted?
使用 cygwin 终端,我的 .bashrc 中有以下内容:
export WINHOME="/cygdrive/c/Users/userName"
export TUTORING="$WINHOME/Desktop/Coding\ Projects/Tutoring/"
当我打开 cygwin 时,会发生以下情况:
$ cd $TUTORING
-bash: cd: too many arguments
然后我尝试了:
$ echo $TUTORING
/cygdrive/c/Users/userName/Desktop/Coding\ Projects/Tutoring
复制粘贴 echo 的输出:
cd /cygdrive/c/Users/userName/Desktop/Coding\ Projects/Tutoring
这似乎有效。知道为什么 cd 对我的变量有问题吗?
使用双引号转义整个路径。
因为space在路径里面,cd命令认为有几个参数
分词发生在变量扩展之后。来自 man bash
:
The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting.
The shell treats each character of IFS as a delimiter, and splits the results of the other expansions into words using these characters as field terminators.
正确的做法是使用双引号:
export TUTORING="$WINHOME/Desktop/Coding Projects/Tutoring/"
cd "$TUTORING"
使用 cygwin 终端,我的 .bashrc 中有以下内容:
export WINHOME="/cygdrive/c/Users/userName"
export TUTORING="$WINHOME/Desktop/Coding\ Projects/Tutoring/"
当我打开 cygwin 时,会发生以下情况:
$ cd $TUTORING
-bash: cd: too many arguments
然后我尝试了:
$ echo $TUTORING
/cygdrive/c/Users/userName/Desktop/Coding\ Projects/Tutoring
复制粘贴 echo 的输出:
cd /cygdrive/c/Users/userName/Desktop/Coding\ Projects/Tutoring
这似乎有效。知道为什么 cd 对我的变量有问题吗?
使用双引号转义整个路径。
因为space在路径里面,cd命令认为有几个参数
分词发生在变量扩展之后。来自 man bash
:
The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting.
The shell treats each character of IFS as a delimiter, and splits the results of the other expansions into words using these characters as field terminators.
正确的做法是使用双引号:
export TUTORING="$WINHOME/Desktop/Coding Projects/Tutoring/"
cd "$TUTORING"