How to Fix Bash: Bad Substitution in Linux?

In Linux, bash is the scripting language utilized to automate repetitive tasks. While working in the bash script, the user may face the error of “bad substitution.” The error is typically caused when the wrong syntax is utilized such as using curly brackets instead of simple brackets or repetition of unwanted characters.

This post will demonstrate the major reasons and the solutions for the error “bad substitutions” in bash.

  • Reason 1: Curly Brackets are Used Instead of Brackets
    • Solution: Use the Simple Brackets
  • Reason 2: Repetition of Unwanted Characters
    • Solution: Avoid Repetition 
  • Reason 3: Extra White Spaces
    • Solution: Remove White Spaces

Reason 1: Curly Brackets are Used Instead of Brackets

The major reason for the bad substitutions in a bash script is that the user is using the wrong syntax such as curly brackets instead of simple brackets. As the following script is trying to retrieve the permissions for the “Henry” directory, but the syntax for storing the command in the variable is wrong:

#!/bin/bash

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

If the above script is executed, the “bad substitution” is displayed.:

$ bash script.sh

Solution: Use the Simple Brackets

For storing the command in the variable, simple brackets are used instead of curly brackets. Let’s apply it to the script and check the execution:

#!/bin/bash

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

Running the above script will not display the “bad substitution” error because the correct syntax is used:

$ bash script.sh

The permission for the “Henry” directory “rwxrwxr-x” is displayed 

Reason 2: Repetition of Unwanted Characters

Another reason for the error “bad substitution” is the repetition of unwanted characters. Take a look at the following script, in which the “pwd” command is stored in the “command” variable, and the echo command prints the “command” variable. But the $ sign is used twice, and the user will face the error of “bad substitution”:

#!/bin/bash

command=$(pwd)
echo "The Current Directory is ${$command}"

Run the above script to test it:

$ bash script.sh

Line 4 encountered a “bad substitution error.”

Solution: Avoid Repetition

The user can avoid repetition and use the correct syntax in the script to be utilized. Removing the $ sign in the following script will resolve the problem and runs the script perfectly:

#!/bin/bash

command=$(pwd)
echo "The Current Directory is ${command}"

Save the script and run it to check the results:

$ bash script.sh

The current directory “home/itslinuxfoss” is printed.

Reason 3: Extra White Spaces

The third situation that may encounter the “bad substitution” error is using extra white spaces. The below-given script executes the command to display the file permission for the “file.txt” that is stored in the “command” variable. While printing the “command” variable through echo, extra white space is added:

#!/bin/bash

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

Run the above script in the terminal to check it:

$ bash script.sh

Line 4 encountered an error of “bad substitution.”

Solution: Remove White Spaces

The problem can be resolved by removing any extra white spaces as done in the following script:

#!/bin/bash

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

Now, save the above script and execute it in the terminal:

$ bash script.sh

The error is removed, and permission “rw-rw-r” for the file “file.txt” is printed on the screen.

Conclusion

The error “bad substitution”  is the wrong syntax, repetition of unwanted characters, and extra white spaces in the bash script. To avoid such errors, use the correct syntax, avoid repetition of characters, and remove extra white spaces from the bash script.

This write-up illustrated the reasons and the solutions for the error “bad substitution” in a bash script.