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.
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
whichis useless, both in interactive shells and in scripts. Usetypeinstead.type quotewill tell you it's a function, and even shows the function definition. Runhelp typefor more on thetypebuiltin. – 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
-
2In my Ubuntu 12.04 this function is really described in
/etc/bash_completion, which is sourced by~/.bashrcby default, not by/etc/bash.bashrcwhere the section regarding/etc/bash_completionis 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
quoteappears in/etc/profile:grep '\bquote\b' /etc/profileNo. Go to next step.
What are the files sourced by
/etc/profile?grep '[[:space:]]\.[[:space:]]' /etc/profileI have
$i(need to look into the source for what this sources, but in this case it's the files/etc/profile.d/*.shif any (and if readable) and/etc/bash.bashrc. Looking in/etc/bash.bashrc.- Is
quotein/etc/bash.bashrc? yes/no , etc...
-
quotedoes actually escape single quotes, so it is usable by the shell. It works just as well asprintf %qfor this use-case. – Flimm Oct 9 '13 at 8:48 -
-
@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 likeExpected: ~/.bashrc,quote: ~/.bashrcandprintf %q: /home/your_username/.bashrc. – Rockallite Feb 26 '17 at 4:46 -
1
-
1If 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.
-
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
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