Bash single quote escape

Escaping Single Quotes Within Single Quotes in Bash

This article introduces how to escape single quotes within single quotes within single quotes in Bash language on Linux PC/server command line. So, if there's a single quote inside code enclosed by <code>'''</code>...

Shou Arisaka
2 min read
Nov 13, 2025

This article introduces how to escape single quotes within single quotes within single quotes in Bash language on Linux PC/server command line.

Image

Escaping single quotes in bash is as follows:

'\''

So, how do you escape if there’s a single quote inside code enclosed by '''?

'\''\'\'''\''

I also wrote code like this and actually created a situation with single quotes within single quotes within single quotes, but it seems that escaping multi-layered single quotes is more complex than expected.

cd "${1}" && ls | xargs -I {} bash -c ' dirname="$( echo "{}" | awk '\''{print $10}'\'' )" ; [[ -d "${dirname}" ]] && [[ ! "${dirname}" =~ ^\.\.$ ]] && echo "{}" | awk -v dirname="${dirname}" '\''{ " du -h "dirname" | tail -1 | awk '\''\'\'''\''{ print $1 }'\''\'\'''\'' " |& getline $11 ; print $0 }'\''  ' ; cd - # great

If you want to know deeper single quote escape sequences, the following page and script will be helpful.

quotify(){

cat << 'EOT' > ${HOME}/quotify ; chmod 755 ${HOME}/quotify ; ${HOME}/quotify
#!/usr/bin/perl -pl
s/'/'\\''/g;
$_ = qq['$_'];
EOT

}

bash - How to escape single quotes within single quoted strings? - Stack Overflow

Share this article

Shou Arisaka Nov 13, 2025

🔗 Copy Links