[Linux] How to escape single quote in 'sh -c'
Sometimes, you need to run a command in ‘sh -c’ and to avoid its subshell expression is evaluated in parent shell, you need to put the command string into a single quote, like:
|
|
However, for some commands in the command string, you need to use single quote
such as awk
, and how do you escape the single quote in the command string?
If you use a backslash to escape the single quote as we do normally in bash, it will look like this:
|
|
It will not work, and show errors like:
|
|
The correct way to escape the single quote in the command string is to use 3 single quotes, like this:
|
|
This will work as expected and print world
.
So that’s it. Use '\''
to escape a single quote in the command string of sh -c
.
Happy Programming. 😃
Last modified on 2025-05-04