Escape Single Quotes in Shell Script

How to escape single quotes within single quoted strings

If you really want to use single quotes in the outermost layer, remember that you can glue both kinds of quotation. Example:

 alias rxvt='urxvt -fg '"'"'#111111'"'"' -bg '"'"'#111111'"'"
# ^^^^^ ^^^^^ ^^^^^ ^^^^
# 12345 12345 12345 1234

Explanation of how '"'"' is interpreted as just ':

  1. ' End first quotation which uses single quotes.
  2. " Start second quotation, using double-quotes.
  3. ' Quoted character.
  4. " End second quotation, using double-quotes.
  5. ' Start third quotation, using single quotes.

If you do not place any whitespaces between (1) and (2), or between (4) and (5), the shell will interpret that string as a one long word.

How to escape a single quote in single quote string in Bash?

echo 'I\'m a student'

does not work. But the following works:

echo $'I\'m a student'

From the man page of bash:

A single quote may not occur between single quotes, even when preceded
by a backslash.

....

Words of the form $'string' are treated specially. The word
expands to string, with backslash-escaped characters replaced as
specified by the ANSI C standard.

Escape single quotes in shell invocation

Try this:

ExecStart=sh -c 'sqlite3 dbfile.db '\''SELECT "The db value is: "||value FROM table'\'' > output.log'

I used to use mysql and double quotes work as well. You can also give it a shot:

ExecStart=sh -c 'sqlite3 dbfile.db "SELECT \"The db value is: \"||value FROM table" > output.log'

Escape single quotes in shell script

For this not too big command I would not overcomplicate it and stick to the original double quotes, and escape just the $ which should not be expanded locally.

ssh_command "file=\$(hostname)_server-setup_$(date +%Y-%m-%d).tar.gz && cd /var && tar -zcvf \$file ini | wc -l | xargs printf 'Num files: %d, File: \$file'" 

How to escape single quote in sed?

Quote sed codes with double quotes:

    $ sed "s/ones/one's/"<<<"ones thing"   
one's thing

I don't like escaping codes with hundreds of backslashes – hurts my eyes. Usually I do in this way:

    $ sed 's/ones/one\x27s/'<<<"ones thing"
one's thing

single quote escaping in bash within single quoted string

Since you can't escape characters within the single-quoted string, you have to do it outside of it:

~$ echo 'abc'\''cde'
abc'cde

Alternatively, use a double-quoted string (requires escaping $)

DATA="
GATEWAY=\$(localcli network ip route ipv4 list | grep default | awk '{print \$3}')
"

How to escape single and double quotes in bash

Use a here-document, and use command subsitution to turn it into an argument.

script "$(cat <<'EOF'
I can't use "test" text
EOF)"

Or

Escape single quote in command argument to sh -c

You're looking for:

sh -c '7z a Aa.zip "B'\''b.txt"'

This: '\'' is an escaped ' as a part of the string. You need that for the sh command itself. Once you've started running the command, leaving the ' unmatched causes a problem, so you need to put it inside of a string.



Related Topics



Leave a reply



Submit