Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do zsh or bash have quotes that are convenient for English text?

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.

like image 280
Owen Avatar asked Oct 14 '12 17:10

Owen


People also ask

What are Bash quotes?

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.

Should you quote variables in Bash?

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.

Does Bash echo need quotes?

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.

What are quotation marks used for in Bash?

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.


2 Answers

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.

like image 197
ZyX Avatar answered Nov 06 '22 07:11

ZyX


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
  • the \EOF instead of just EOF makes $ not be magic
like image 24
DigitalRoss Avatar answered Nov 06 '22 05:11

DigitalRoss