Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

echo - Syntax error: Bad substitution

Tags:

linux

bash

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 :)

like image 290
andrej Avatar asked Dec 02 '13 08:12

andrej


1 Answers

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 '=' '-')
like image 139
devnull Avatar answered Oct 12 '22 13:10

devnull