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.

  • it just adds the quotes – Hakeem Wahab Oct 7 '13 at 16:18
  • 1
    Just poking around, I found a utility called shell-quote which is similar to quote, but has some uses with things like ssh and for debugging bash scripts. Thought I'd include it here as a footnote. 'linux.die.net/man/1/shell-quote' – Joe Oct 14 '13 at 1:58
  • Feels like a WTF to have such a common word, quote, used for such obscure purpose... No prefix to mark it "internal", no documentation, nothing, unless you go digging in implementation details of the advanced completion mechanics of the distro. I find this a little disturbing. – hyde Nov 26 '14 at 6:59
  • It becomes even more obscure for the person, whose native language isn't English. Like me, for example. :) – whtyger Nov 27 '14 at 8:13

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

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.

| improve this answer | |
  • 3
    which is useless, both in interactive shells and in scripts. Use type instead. type quote will tell you it's a function, and even shows the function definition. Run help type for more on the type builtin. – geirha Oct 7 '13 at 19:10
  • You're right, thanks. I saw the answer above which also used type to find out what it is. Well, thanks to 'which' I knew that it wasn't an executable in /bin/ or so, because it did not return any path. I think it was still kind of useful. But you are right, I'll add type. – verpfeilt Oct 7 '13 at 20:01
  • 2
    In my Ubuntu 12.04 this function is really described in /etc/bash_completion, which is sourced by ~/.bashrc by default, not by /etc/bash.bashrc where the section regarding /etc/bash_completion is commented out. Also /usr/share/bash-completion/ folder is absent in Ubuntu 12.04. – whtyger Oct 8 '13 at 7:17

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...
| improve this answer | |
  • quote does actually escape single quotes, so it is usable by the shell. It works just as well as printf %q for this use-case. – Flimm Oct 9 '13 at 8:48
  • @Flimm use at your own risks! – gniourf_gniourf Oct 9 '13 at 14:26
  • @gniourf_gniourf I think printf %q does not properly handle tilde (~). Try this: my_str='~/.bashrc'; echo ' Expected:' "$my_str"; bash -c "echo ' quote:' $(quote "$my_str")"; bash -c "echo 'printf %q:' $(printf '%q\n' "$my_str")". You would see something like Expected: ~/.bashrc, quote: ~/.bashrc and printf %q: /home/your_username/.bashrc. – Rockallite Feb 26 '17 at 4:46
  • 1
    @Rockallite: I just tried it and it works a expected... – gniourf_gniourf Feb 26 '17 at 8:15
  • If you want to skip straight to the source, you can use (shopt -s extdebug; declare -F quote), which tells you the function name, line number it is defined at, and filename it is defined in. – wjandrea Jan 12 '18 at 2:58

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.

| improve this answer | |
  • What it does is clear - but why you would want to do it may not be. For the avoidance of doubt: it would be used for taking a variable such as $INPUT, that may contain spaces, quotes or other characters, and returning a string that is definitely only treated as a single argument by bash. – Ben XO Sep 12 '16 at 10:00

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.