If I use single quotes, words with apostrophes ("don't") are annoying to escape:
'Don'"'"'t do that'
If I use double quotes, dollar signs and exclamation points trip it up:
"It cost like \$1000\!"
Is there another kind of quoting I can use?
edit: I should also add that I would like to pass this string directly as a command line argument, rather than storing it in a variable. To this end I tried, using DigitalRoss's solution,
$ echo "$(cat << \EOF
Don't $worry be "happy".
EOF)"
but get
dquote cmdsubst>
after hitting enter :/ . So at this point ZyX's suggestion of setopt rcquotes
looks to be the most convenient.
In a Bash script, when we quote a string, we set it apart and protect its literal meaning. Certain programs and utilities reinterpret or expand special characters in a quoted string. An important use of quoting is protecting a command-line parameter from the shell, but still letting the calling program expand it.
General rule: quote it if it can either be empty or contain spaces (or any whitespace really) or special characters (wildcards). Not quoting strings with spaces often leads to the shell breaking apart a single argument into many.
Although not necessary, it is a good programming practice to enclose the arguments passed to echo in double or single quotes. When using single quotes '' the literal value of each character enclosed within the quotes will be preserved. Variables and commands will not be expanded.
The double quotes allowes to print the value of $SHELL variable, disables the meaning of wildcards, and finally allows command substitution. The single quote ( 'quote' ) protects everything enclosed between two single quote marks. It is used to turn off the special meaning of all characters.
With zsh you may do
setopt rcquotes
. Then ASCII apostrophes are escaped just like this:
echo 'Don''t'
. Or setup your keymap to be able to enter UTF apostrophes, they have no issues with any kind of quotes (including none) in any shell:
echo 'Don’t'
. Third works both for zsh and bash:
echo $'Don\'t'
.
Neither first nor third can narrow down quote to a single character, but they are still less verbose for non-lengthy strings then heredocs suggested above. With zsh you can do this by using custom accept-line widget that will replace constructs like 'Don't'
with 'Don'\''t'
. Requires rather tricky regex magic that I can write only in perl; and is probably not the best idea as I can’t pretend I can cover all possible cases before they will hit. It won’t in any case touch any scripts.
I like the direction Zsolt Botykai is going. Here is an example that works with any Posix shell. (I also verified that it survived its paste into the SO server.)
$ read -r x << \EOF
Don't $worry be "happy".
EOF
$ echo $x
Don't $worry be "happy".
The things that make this work;
-r
will make \
not be magic\EOF
instead of just EOF
makes $
not be magicIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With