Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
    
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
add comment

3 Answers 3

up vote 6 down vote accepted

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 ist eine Funktion.              # "quote is a function" (german language)
quote ()                              # definition of quote
{ 
    local quoted=${1//\'/\'\\\'\'};
    printf "'%s'" "$quoted"
}
chocobai@pc:~$

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.

share|improve this answer
1  
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
1  
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
add comment

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...
share|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
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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