既然建立了以下命令的别名?
Since establishes an alias of the following command?
您好,我正在尝试使用 gitbash
在以下 别名 中建立,但不能:
alias dirpwd='$(`pwd | xargs dirname | xargs basename -a`)' && echo -e $dirpwd;
输出为:
bash: c: command not found
为什么?
谢谢大家!!
发生了什么:
反引号和 $()
的工作方式相同。它们中的命令将被执行,结果输出将被视为您直接输入的结果。
假设您在 /top/c/bottom
。
反引号内的 pwd | xargs dirname | xargs basename -a
部分输出 c
。
Bash 用它的输出替换反引号部分,导致命令 $(c)
。
现在 bash 尝试在 $(...)
中执行命令,但 c
不是命令,因此出现错误 bash: c: command not found
.
第一次修复:
我猜你只是想写
alias dirpwd="pwd | xargs dirname | xargs basename -a"
错误:
有一个隐藏的错误。 xargs
在空格处拆分,可以将多个参数传递给 dirname
和 basename
.
示例:假设您在 /top/a b c/
。 xarg
创建以下命令并输出
dirname "/top/a" "b" "c/"
/top
.
.
第二次修复:
使用 $()
而不是 xargs
。
alias dirpwd='basename "$(dirname "$PWD")"'
尝试解决方案:
alias dirpwd="pwd | xargs dirname | xargs basename -a"
您好,我正在尝试使用 gitbash
在以下 别名 中建立,但不能:
alias dirpwd='$(`pwd | xargs dirname | xargs basename -a`)' && echo -e $dirpwd;
输出为:
bash: c: command not found
为什么?
谢谢大家!!
发生了什么:
反引号和 $()
的工作方式相同。它们中的命令将被执行,结果输出将被视为您直接输入的结果。
假设您在 /top/c/bottom
。
反引号内的 pwd | xargs dirname | xargs basename -a
部分输出 c
。
Bash 用它的输出替换反引号部分,导致命令 $(c)
。
现在 bash 尝试在 $(...)
中执行命令,但 c
不是命令,因此出现错误 bash: c: command not found
.
第一次修复:
我猜你只是想写
alias dirpwd="pwd | xargs dirname | xargs basename -a"
错误:
有一个隐藏的错误。 xargs
在空格处拆分,可以将多个参数传递给 dirname
和 basename
.
示例:假设您在 /top/a b c/
。 xarg
创建以下命令并输出
dirname "/top/a" "b" "c/"
/top
.
.
第二次修复:
使用 $()
而不是 xargs
。
alias dirpwd='basename "$(dirname "$PWD")"'
尝试解决方案:
alias dirpwd="pwd | xargs dirname | xargs basename -a"