【发布时间】:2021-04-21 11:13:39
【问题描述】:
我发现很难将 printf 语句嵌套在别名中。我有许多我想提供的帮助主题(只是一些有用的提示,用于在我忘记语法时使用)。我发现printf 需要\ 转义为\\ 和% 转义为%%。但是,我的问题更多是与' 和"
alias helpx='printf "A note about 'vim'.\n"'
=> A note about vim. # The ' are ignored.
alias helpx="printf 'A note about 'vim'.\n'"
=> A note about vim. # The ' are ignored.
alias helpx='printf "A note about \'vim\'.\n"' # Invalid syntax
alias helpx='printf "A note about \"vim\".\n"'
=> A note about "vim". # Some progress, I can now get " here
如何在上面的笔记中获取' 字符?
【问题讨论】:
-
这与
printf无关。这是关于嵌套引用。如果将helpx定义为一个函数会容易得多。 -
我同意功能会更容易,谢谢。奇怪的是,bash 中的别名和函数如此接近,它们在功能上几乎可以互换,没有太大区别。
-
man bash说 “关于别名的定义和使用的规则有些混乱。[...] 几乎所有用途,别名都被 shell 函数所取代。”.