我在 .bash_aliases 中有一个别名,当我触发它时,它会给我错误的输出。
alias print="echo a b | awk '{print $2}'"
错误输出:
slazer@ubuntu:~/Desktop/xpalenik_pb161/hw03/examples/extra$ print
a b
预期输出:
slazer@ubuntu:~/Desktop/xpalenik_pb161/hw03/examples/extra$ echo a b | awk '{print $2}'
b
其他信息:
slazer@ubuntu:~/Desktop/xpalenik_pb161/hw03/examples/extra$ cat ~/.bashrc | grep 'alias print'
slazer@ubuntu:~/Desktop/xpalenik_pb161/hw03/examples/extra$ cat ~/.bash_aliases | grep 'alias print'
alias print="echo a b | awk '{print $2}'"
slazer@ubuntu:~/Desktop/xpalenik_pb161/hw03/examples/extra$ alias | grep 'alias print'
alias print='echo a b | awk '\''{print }'\'''
slazer@ubuntu:~/Desktop/xpalenik_pb161/hw03/examples/extra$ print
a b
slazer@ubuntu:~/Desktop/xpalenik_pb161/hw03/examples/extra$ echo a b | awk '{print $2}'
b
slazer@ubuntu:~/Desktop/xpalenik_pb161/hw03/examples/extra$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.3 LTS
Release: 14.04
Codename: trusty
我尚未安装所有更新。这会是个问题吗?
答案1
您需要转义$
以防止它被 shell 扩展;由于它扩展为无,您的别名将等同于
alias print="echo a b | awk '{print }'"
尝试一下
alias print="echo a b | awk '{print \$2}'"