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 '
:
'
End first quotation which uses single quotes."
Start second quotation, using double-quotes.'
Quoted character."
End second quotation, using double-quotes.'
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
Different File Owner Inside Docker Container and in Host MAChine
Bash - Find Files Older Than X Minutes and Move Them
Bash Shell Script - Check for a Flag and Grab Its Value
How to Copy Text from My Xterm Without a Mouse
Sanitize Environment with Command or Bash Script
How to Fetch Java Version Using Single Line Command in Linux
How to Decrease the Size of Generated Binaries
Why Do I Get Permission Denied When I Try Use "Make" to Install Something
How to Append to a File Using X86-64 Linux System Calls
Symbolic Link to a Hook in Git
How to Clean Caches Used by the Linux Kernel
Importing a Cmake Project into Eclipse Cdt
How to Pipe Output from Grep to Cp
Why Does Perf Stat Show "Stalled-Cycles-Backend" as <Not Supported>