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.
sh
explicitly on the command line? If so, consider making the script executable instead and running it without an explicit interpreter, or run it withbash
. – Kusalananda May 30 '21 at 11:18sh
– Phantom May 30 '21 at 15:00