Linux – What is a quote command?

What is a quote command?… here is a solution to the problem.

What is a quote command?

Using the bash interactive terminal, the output of quote a is ‘a' as expected. But references that use bash -c 'quote a' or don't work in shell scripts, giving the error bash: line 1: quote: command not found. quote is not executable, I can’t find quote in the bash built-in reference, so where does this command come from?

File directory:

#
# ~/.bashrc
#

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

stty -ixon

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

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

Solution

quote is a helper function in /usr/share/bash-completion/bash_completion

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

I won’t use it because it’s only available in the interactive shell when enabled is complete.

If you want to escape special characters in your script, you can use ${var@Q} or printf %q instead.

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

Related Problems and Solutions