jon*_*hul 0 bash alias escaping
我正在尝试在我的.bashrc文件中设置一个别名,如下所示:
clear && printf '\033[3J'
Run Code Online (Sandbox Code Playgroud)
但是以下方法不起作用
alias clall= "clear && printf \'\033[3J\'"
alias clall= "clear \&\& printf \'\\033\[3J\'"
Run Code Online (Sandbox Code Playgroud)
关于别名的一般规则是,如果您对如何使用它们(或它们是否足够满足您的用途)有疑问,则应改用函数。函数为您提供了全部功能(就此而言,具有更多的功能),并且不需要任何引用/转义语法:
clall() { clear && printf '\033[3J'; }
Run Code Online (Sandbox Code Playgroud)
也就是说,一种指定所需别名的方法是以下bash扩展语法:
# use $'' to make \' and '' valid/meaningful
alias clall=$'clear && printf \'\\033[3J\''
Run Code Online (Sandbox Code Playgroud)
... $''用于允许在单引号内转义单引号(和反斜杠);在正常''报价下,包含的反斜杠为文字。更加POSIX-y的方法是:
# use '"'"' to put a literal single-quote inside syntactic single-quotes
alias clall='clear && printf '"'"'\033[3J'"'"''
Run Code Online (Sandbox Code Playgroud)
...或者,如果(如此处所示)您没有双引号内特殊的语法:
# ...or just use double quotes for the whole thing, absent a reason not to
# ...using command substitution, paramater expansion, etc. would be such a reason.
alias clall="clear && printf '\033[3J'"
Run Code Online (Sandbox Code Playgroud)