"quote" command in the shell

What is the usage of the command quote? I haven't found any info about it, it isn't among the executable files in /bin folders, and it cannot be found among Bash built-ins. It seems that it only prints its first parameter, like an echo command and nothing more.

4

3 Answers

What is it?

I noticed that this command does not work in my shell (fish, friendly interactive shell). It seems like it does only work in bash (Ubuntu's default).

chocobai@pc ~> /bin/bash
chocobai@pc:~$ quote asdf
'asdf'chocobai@pc:~$
chocobai@pc:~$ type quote
quote is a function.
quote ()
{ local quoted=${1//\'/\'\\\'\'}; printf "'%s'" "$quoted"
}

What does it do? What can it be used for?

It adds the quotes but no newline. It also escapes single quotes in a way that's suitable for bash. It can be useful in scripts to quote a variable or some other kind of string. You need this for example for paths/parameters with spaces. Although there are other ways to do this.

It's really strange I could not find any documentation (in the web) about it. But well, it's easy to see what it does.

3

quote is a function that is defined (here on my Debian system, but I guess it's the same on Ubuntu) in the file /usr/share/bash-completion/bash_completion, which itself is sourced by /etc/bash.bashrc at Bash's startup.

I would never use this function! If you need to quote stuff so as to be safely usable by a shell, please use printf with the %q modifier, as:

printf '%q\n' "Hello my friend I like 'single quotes' as well as \"double quotes\""

In fact, even this is very rarely used, there are always better strategies for high-level stuff as we, users, usually do. This quote thing is used internally by some obscure stuff we don't even want to know about. This quote function is probably a vendor/distribution-specific (read Debian-specific) and is probably not portable at all, and might even change in future releases.

Edit. I've just checked on an Ubuntu 12.04 system, and the quote function is defined in /etc/bash_completion, sourced by /etc/bash.bashrc, itself sourced by /etc/profile.

How did I determine this? using a little of heuristic:

  • Check if quote appears in /etc/profile:

    grep '\bquote\b' /etc/profile

    No. Go to next step.

  • What are the files sourced by /etc/profile?

    grep '[[:space:]]\.[[:space:]]' /etc/profile

    I have $i (need to look into the source for what this sources, but in this case it's the files /etc/profile.d/*.sh if any (and if readable) and /etc/bash.bashrc. Looking in /etc/bash.bashrc.

  • Is quote in /etc/bash.bashrc? yes/no , etc...
5

quote is a function:

quote ()
{ local quoted=${1//\'/\'\\\'\'}; printf "'%s'" "$quoted"
}

This function is defined somewhere in a bash initialization file. More precisely, if you are using Ubuntu 13.04, you can find it in /usr/share/bash-completion/bash_completion at the line 142.

Use the following command to check it:

type quote

Its purpose is evidently clear.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like