I try a basic string replacement in a bash script:

x="I love Linux"
echo "${x/Linux/Unix}"

It works fine on my mac, but does not work on my server. I tried different examples from various sites, but I always get the error: Bad substitution

My bash version: GNU bash, Version 4.4.12(1)-release (x86_64-pc-linux-gnu)

What am I doing wrong?

  • 3
    How are you actually running this script. I'm guessing that you are using sh explicitly on the command line? If so, consider making the script executable instead and running it without an explicit interpreter, or run it with bash.
    – Kusalananda
    May 30, 2021 at 11:18
  • 1
  • @Kusalananda Thank you very much! My mistake was to run the script with sh
    – Phantom
    May 30, 2021 at 15:00

1 Answer 1


echo "${x/Linux/Unix}" is correct syntax in bash since version 2.0 which was released in 1996.

Bash does not emit the exact error message Bad substitution. Its message when it encounters something that looks almost, but not quite, like a parameter substitution, is slightly different:

$ bash -c '${}'
bash: ${}: bad substitution

On the other hand, dash, which is the implementation of /bin/sh on many Linux systems, does emit this exact message:

$ dash -c 'echo "${x/Linux/Unix}"'
dash: 1: Bad substitution

The error is enough of a hint that, as Kusalananda noticed, you aren't actually running this script as a program: you're running some program, presumably sh or /bin/sh, and telling it to run this script. Your script is a correct bash program but not a correct sh program, so that won't work on systems where sh is a different dialect that doesn't have the same feature set as bash.

Your script is a standalone program. The shebang line at the top of the script tells the system what interpreter to use. To run the script, just type its path, or its file name if it's on the command search path ($PATH). Like any other executable program, whether it's a script (has a shebang line) or not (a binary that the kernel executes on its own1), it needs to have execution permissions (chmod +x path/to/script). If for some reason you have a script which is not a standalone program and you want to run it with bash, run bash path/to/script, not sh path/to/script.

1 Or with other means of conveying how the file is to be executed, such as the binfmt_misc mechanism on Linux.

  • Thank you very much for your answer. I ran the script with sh - that was my mistake. With bash path/to/script it works fine. How can I make a standalone program from my script. When I try to run the script with path/to/script without the bash, I get the error -bash: /path/to/script/test.sh: Keine Berechtigung Keine Berechtigung (German) = No permission
    – Phantom
    May 30, 2021 at 15:02
  • 1
    @Phantom, you need to add the execute permission on the script, chmod +x scripname.sh
    – ilkkachu
    May 30, 2021 at 15:37
  • @ilkkachu Thank you very much! That's great
    – Phantom
    May 30, 2021 at 19:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .