Fundamentals 10 min read

Mastering Shell Command Substitution: Backticks vs $() and Escape Tricks

This article explains shell command substitution, comparing backticks and the modern $(…) syntax, details their handling of special characters, demonstrates nesting and backslash escaping tricks across various shells, and provides practical examples and solutions for reliable quoting and path handling.

ITPUB
ITPUB
ITPUB
Mastering Shell Command Substitution: Backticks vs $() and Escape Tricks

What is command substitution?

Command substitution lets a shell replace a command with its standard output. In bsh, ksh, bash and csh the classic syntax is `command`. The back‑ticks are the characters located left of the 1 key on most keyboards.

Basic example

# echo we are now in `pwd`

The shell runs pwd, substitutes its output, and prints something like we are now in /root.

Modern $(…) syntax

The same command can be written as: # echo we are now in $(pwd) In ksh and bash this form is preferred because it is more readable, avoids ambiguity, and handles nesting without extra escaping.

Why prefer $(…) over back‑ticks?

Readability : $(…) is clearer and does not look like a single quote.

Nesting : Nested $(…) constructs are straightforward; back‑ticks require escaping inner back‑ticks with \.

Backslash handling : Inside $(…) backslashes need fewer escapes, which simplifies complex commands.

Although $(…) works in most modern environments, back‑ticks remain useful for maximum compatibility with older Bourne‑style shells that may not have $(…) support.

Special characters inside substitution

Back‑ticks treat the following as special: $, backslash \, double quote ", single quote ', and the back‑tick itself. The $(…) form treats $, double quote, and single quote as special.

Escaping backslashes – a classic puzzle

Consider the command: # echo `echo \\` The processing steps are:

The outer shell sees `echo \\`. Because \ is special inside back‑ticks, it becomes \ for the inner command.

The inner shell runs echo \. In ksh the default echo treats \ as an escape, so it outputs a single backslash.

The outer echo receives that single backslash and prints it.

To output two backslashes you need a staggering number of backslashes in the original line (20 in ksh, 18 in bsh).

Techniques to simplify backslash handling

Strong quoting (single quotes) prevents the first‑pass shell from interpreting backslashes.

echo -E option disables escape‑sequence processing in ksh.

Using $(…) avoids the need for extra escaping when the inner command runs.

Example with $(…): # echo -E $(echo -E '\') Result: \ – the simplest form.

Portability across shells

Older bsh does not support $(…); its back‑tick behavior is similar to ksh but its echo always interprets escape sequences. bash behaves like ksh with echo not interpreting escapes by default (equivalent to echo -E). tcsh lacks $(…) and treats backslashes as ordinary characters inside single or double quotes, while echo also treats them as ordinary.

Practical example: escaping a Windows path

Goal: turn c:\tmp into c:\\tmp for a script.

# dospath='c:\tmp'
escaped=`echo $dospath | sed 's/\\/&&/'`
# or, more portable:
escaped=$(echo $dospath | sed 's/\/&&/')
# finally print:
echo -E $escaped

The first form works on many shells; the second uses $(…) for better readability.

Summary of quoting mechanisms

Strong quoting ( '...'): only the single‑quote itself is special.

Weak quoting ( "..."): special characters are ", backslash \, dollar $, and back‑tick `.

Escape ( \): any character, including backslash, can be escaped.

Class‑quote `` : works in bsh/ksh/bash/csh; special characters are back‑tick, $, \, and both quotes.

Class‑quote $() : works in ksh/bash; special characters are $ and both quotes.

Echo behavior by shell

bsh

: always interprets escape sequences; cannot disable. ksh: interprets escape sequences by default; -E disables. bash: does not interpret escape sequences by default; -e enables.

Test environment

CentOS 4.2 x86‑64

bash 3.00.15(1)

ksh pdksh‑5.2.14‑30.3

bsh ash‑0.3.8‑20

csh tcsh‑6.13‑9

PS: The article is copyrighted by the ChinaUnix user “woodie”.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ShellBashescapingcommand substitutionquotingksh
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.