cd:参数太多-空格正确转义了+引用了吗? [英] cd: too many arguments - spaces properly escaped + quoted?

查看:112
本文介绍了cd:参数太多-空格正确转义了+引用了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用cygwin终端,我的.bashrc文件中包含以下内容:

Using cygwin terminal, I have the following in my .bashrc:

export WINHOME="/cygdrive/c/Users/userName"
export TUTORING="$WINHOME/Desktop/Coding\ Projects/Tutoring/"

当我打开cygwin时,会发生以下情况:

When I open cygwin, here's what happens:

$ cd $TUTORING
-bash: cd: too many arguments

所以我尝试了:

$ echo $TUTORING
/cygdrive/c/Users/userName/Desktop/Coding\ Projects/Tutoring

复制粘贴回显的输出:

cd /cygdrive/c/Users/userName/Desktop/Coding\ Projects/Tutoring 

这似乎可行.知道cd为什么我的变量有问题吗?

And this seems to work. Any idea why cd has a problem with my variable?

推荐答案

在变量扩展后会发生单词拆分.来自 man bash :

Word splitting happens after variable expansion. From man bash:

shell扫描参数扩展,命令替换和算术扩展的结果,这些结果在双引号中没有出现,以进行词拆分.

The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting.

shell将IFS的每个字符视为定界符,并使用这些字符作为字段终止符将其他扩展的结果拆分为单词.

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.

正确的方法是使用双引号:

The correct way is to use double quotes:

export TUTORING="$WINHOME/Desktop/Coding Projects/Tutoring/"
cd "$TUTORING"

这篇关于cd:参数太多-空格正确转义了+引用了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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