在bash中的一个别名中转义单引号 [英] escaping single quotes inside an alias in bash

查看:263
本文介绍了在bash中的一个别名中转义单引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的.bashrc文件中设置一个别名,如下所示:

I am trying to set up an alias in my .bashrc file like the below:

clear && printf '\033[3J'

但以下不起作用

alias clall= "clear && printf \'\033[3J\'"
alias clall= "clear \&\& printf \'\\033\[3J\'"


推荐答案

关于别名的一般规则是,如果你有一个关于如何使用它们的问题(或者他们是否足够用于你的目的),你应该使用一个功能。一个函数给你所有的功能(相当多的这个问题),并且不需要任何引用/转义语法:

The general rule about aliases is that if you have a question about how to use them (or whether they're capable enough for your purpose), you should be using a function instead. A function gives you all the capability (considerably more, for that matter), and doesn't require any quoting/escaping syntax:

clall() { clear && printf '\033[3J'; }






说,一种方法来指定别名你想要的是以下bash扩展语法:


That said, one way to specify the alias you want is the following bash-extended syntax:

# use $'' to make \' and '' valid/meaningful
alias clall=$'clear && printf \'\\033[3J\''

...其中使用 $''允许单引号(和反斜杠)在单引号内进行转义;在正常''引用下,包含的反斜杠是文字的。更多的POSIX-y方法是:

...which uses $'' to allow single-quotes (and backslashes) to be escaped within single-quotes; under normal '' quoting, contained backslashes are literal. A more POSIX-y approach is:

# use '"'"' to put a literal single-quote inside syntactic single-quotes
alias clall='clear && printf '"'"'\033[3J'"'"''

...或,如果(如这里)你没有任何特殊的双引号的语法:

...or, if (as here) you don't have any syntax that's special inside double quotes:

# ...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'"

这篇关于在bash中的一个别名中转义单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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