Oct 5, 2023 3 min read

How to Fix Bash: Bad Substitution on Linux

Fix Bash: Bad Substitution on Linux with our step-by-step tutorial. Bash is a scripting language used on Linux to automate monotonous operations.

Fix Bash: Bad Substitution on Linux
Table of Contents


Before we begin talking about how to Fix Bash: Bad Substitution on Linux, let's briefly understand – What is Bash?

Bash is a scripting language used on Linux to automate monotonous operations. The user may encounter the "bad substitution" error while interacting with the bash script. The mistake is generally made when the incorrect syntax is used, such as when curly brackets are used in place of plain brackets or when unnecessary characters are repeated.

In this tutorial, you will Fix Bash: Bad Substitution on Linux. We will also address a few FAQs on how to Fix Bash: Bad Substitution on Linux.

Reason 1: Curly Brackets are Used Instead of Brackets

The most common cause of bad substitutions in a bash script is because the user uses incorrect syntax, such as curly brackets instead of simple brackets. The syntax for saving the command in the variable is incorrect, and the following script is attempting to retrieve the permissions for the "Henry" directory:

command=${ls -ld Henry}
echo $command

When the above script is run, the "bad substitution" error appears.

bash script.sh

Solution: Use the Simple Brackets

Simple brackets are used in place of curly brackets to store the command in the variable. Let us apply it to the script and see how it works:

command=$(ls -ld Henry)
echo $command

The "bad substitution" error will not appear when running the script above because the proper syntax is used:

bash script.sh

The permissions for the "Henry" directory are displayed as "rwxrwxr-x".

Reason 2: Repetition of Unwanted Characters

Another cause of the error "bad substitution" is the repetition of characters that aren't needed. Look at the script below, where the echo command outputs the "command" variable and the "pwd" command stores its output in the "command" variable. However, the $ sign is used twice, causing the user to see a "bad substitution" error:

echo "The Current Directory is ${$command}"

Run the script from above to test it:

bash script.sh

There was a "bad substitution" error on line 4.

Solution: Avoid Repetition

By using the proper syntax in the script to be used, the user can prevent repetition. The script will function correctly if the $ symbol is removed from the following code:

echo "The Current Directory is ${command}"

To check the outcomes, run the script after saving it:

bash script.sh

It prints the current directory, "home/vegastack".

Reason 3: Extra White Spaces

The third condition that may result in the "bad substitution" error is the use of excessive white spaces. The script that is provided below runs the command that is specified in the variable "command" for the file "file.txt" to display the file permission. Extra white space is added when writing the "command" variable through echo:

command=$(ls -l file.txt)
echo ${command }

To test it, run the aforementioned script in the terminal:

bash script.sh

There was a "bad substitution" error on line 4.

Solution: Remove White Spaces

The issue can be fixed by eliminating any unnecessary white spaces, as demonstrated in the script below:

command=$(ls -l file.txt)
echo ${command}

Save the script above, then run it in the terminal:

bash script.sh

The problem is fixed, and the screen displays "rw-rw-r" for the file "file.txt" permissions.

FAQs to Fix Bash: Bad Substitution on Linux

What does the "Bad Substitution" error mean in Bash? 

The "Bad Substitution" error typically occurs when using unsupported syntax or incorrect variable expansion in Bash.

Why am I seeing "Bad Substitution" while using curly braces in my Bash script? 

"Bad Substitution" often occurs when you use unsupported syntax, such as empty curly braces or unescaped special characters within the curly braces.

What should I check if I encounter the "Bad Substitution" error in a shell script? 

Check for any incorrect or unsupported variable expansions, such as using an undefined variable or incorrect syntax like double dollar signs ($$) instead of a single one ($).

Can using an old version of Bash lead to the "Bad Substitution" error? 

Yes, using an outdated version of Bash may cause this error. It's recommended to update to a newer version of Bash if possible.

Why am I receiving a "Bad Substitution" error while running my shell script on a different operating system?

Different operating systems may have variations in Bash versions and shell configurations, which can result in the "Bad Substitution" error. Ensure your script is compatible with the target operating system.

Can escaping special characters resolve the "Bad Substitution" error? 

Yes, escaping special characters correctly using backslashes () can often resolve the "Bad Substitution" error. Make sure to escape any special characters that require it.

Is the "Bad Substitution" error specific to Bash only? 

Yes, the "Bad Substitution" error typically pertains to the syntax and variable expansion rules of the Bash shell. Other shells may have different error messages for similar issues.


The bash script's "bad substitution" error is caused by incorrect syntax, the repetition of unnecessary characters, and additional white spaces. Use the proper syntax, reduce character repetition, and eliminate excess white spaces from the bash script to prevent such errors. This tutorial discussed the causes and fixes for the "bad substitution" bash script error.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.