什么是报价命令?

What is the quote command?

使用bash交互式终端,quote a的输出是预期的'a'。但是引用在使用 bash -c 'quote a' 或在 shell 脚本中不起作用,给出错误 bash: line 1: quote: command not found。 quote 不是可执行文件,我在 bash 内置参考中找不到 quote,那么这个命令来自哪里?

bashrc:

#
# ~/.bashrc
#

# If not running interactively, don't do anything
[[ $- != *i* ]] && return

stty -ixon

#source $HOME/.config/xdgrc
#source $HOME/.config/aliasrc

PS1='\[3[38;2;50;255;50m\]\u\[3[0m\]@\[3[38;2;255;70;70m\]\h\[3[0m\]:\w\[3[38;2;50;255;50m\]$\[3[0m\]> '

quote/usr/share/bash-completion/bash_completion 中的辅助函数:

# This function shell-quotes the argument
quote()
{
    local quoted=${1//\'/\'\\'\'}
    printf "'%s'" "$quoted"
}

我不会使用它,因为它仅在启用完成时在交互式 shell 中可用。

如果您想转义脚本中的特殊字符,您可以使用 ${var@Q}printf %q

$ wendys="Where's the beef?"
$ echo "${wendys@Q}"
'Where'\''s the beef?'
$ printf '%q\n' "$wendys"
Where\'s\ the\ beef\?