echo - Syntax error: Bad substitution

bash, linux

Solution

The following line:

str=$(echo "${str// /-}")

is resulting into `Syntax error: Bad substitution` because you are not executing your script using `bash`. You are either executing your script using `sh` or `dash` which is causing the error.

EDIT: In order to fix your script to enable it to work with `sh` and `dash` in addition to `bash`, you could replace the following lines:

# get desired string
str=$(printf "%${leng}s" "-")

# replace empty spaces
str=$(echo "${str// /-}")

with

str=$(printf '=%.0s' $(seq $leng) | tr '=' '-')

Problem

A script with a problem: ``` 1 #!/bin/bash 2 3 skl="test" 4 # get length 5 leng=$(expr length $skl) 6 # get desired length 7 leng=$(expr 22 - $leng) 8 9 # get desired string 10 str=$(printf "%${leng}s" "-") 11 12 # replace empty spaces 13 str=$(echo "${str// /-}") 14 15 # output 16 echo "$str obd: $skl $str" 17 ``` but it outputs: ``` name.sh: 13: Syntax error: Bad substitution ``` please help, thanks I would be very grateful :)

Original source