Echo - Syntax Error: Bad Substitution

echo - Syntax error: Bad substitution

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 '=' '-')

Shell script error - bad substitution - principle

If I run it in terminal via sh test.sh

There's your problem. ${parameter/pattern/string} is bash syntax, not vanilla sh. Run it via bash test.sh (And/or put an appropriate shebang in your script and make it executable so you can just run ./test.sh).

Shell script - Bad substitution Error while running script with sh

To make my comment an answer:

You're running with sh, but your script declares it's a bash script. On many systems sh is not bash, but a lighter shell that doesn't support all bashisms.

Either

  • run with bash test.sh, or
  • mark the file chmod u+x and run ./test.sh to use the shebang line.

bash: bash script gives Bad substitution error

The correct syntax is:

url=$(cat /var/scripts/test.txt | grep -oP '(?<=display_url":")[^"]+')

That being said, to avoid Useless Use Of Cat, try this:

url=$(grep -oP '(?<=display_url":")[^"]+' /var/scripts/test.txt)

From man bash:

Command Substitution
Command substitution allows the output of a command to replace the command name.
There are two forms:

          $(command)
or
`command`

bash : Bad Substitution

The default shell (/bin/sh) under Ubuntu points to dash, not bash.

me@pc:~$ readlink -f $(which sh)
/bin/dash

So if you chmod +x your_script_file.sh and then run it with ./your_script_file.sh, or if you run it with bash your_script_file.sh, it should work fine.

Running it with sh your_script_file.sh will not work because the hashbang line will be ignored and the script will be interpreted by dash, which does not support that string substitution syntax.

bash parameter substitution error - Bad substitution

There is absolutely nothing wrong with the sections of the code you've shown that are meant to make the changes. This can be seen if you use the much simpler:

#!/bin/bash
while read a ; do echo ${a//==/>=} ; done <inputFile.txt

When I run that against your input file, I get what's expected:

pax$ ./myScript.sh
certifi>=2018.1.18
chardet>=3.0.4
coverage>=4.5.1
: : :
smmap2>=2.0.3
urllib3>=1.22
xml2json>=1.1

A couple of things you may want to look at:

  • Add set -x to the script so that all lines are output before execution - this may indicate where a potential problem lies.
  • Within the loop, add the statement echo ".$a." to see what the actual input line is.
  • Within the loop, add the statement echo -n "$a" | tr -d '[\-A-Za-z0-9=.]' to see if there are any unexpected characters (all counts should be zero).
  • Insert the ( set ; env ) | grep -i bash statement at the start of the script to make sure you're running bash (and the correct version of bash).

That last point is particularly important if you're actually running the script with sh setup.py ... as per your comment in the code, since sh and bash are not the same thing. The shebang line normally dictates what shell is used but, if you run it explicitly with sh, it gets bypassed:

pax$ sh myScript.sh
myScript.sh: 2: myScript.sh: Bad substitution

In any case, it's probably a better idea to use sed for this sort of task:

 sed -i 's/==/>=/' inputFile

This will edit inputFile in-place, making the substitutions on the fly. Then you don't have to worry about any possible vagaries of bash or having to move the file yourself after doing the substitution.

Bad Substitution Error from Variables

Change the curly braces to parentheses.

LAST_MONTH="$(date +'%Y%m' -d 'last month')"
LAST_MONTH_HYPHEN="$(date +'%Y-%m' -d 'last month')"

Curly braces are for variable substitution, as in ${var}, equivalent to $var. Parentheses are for command substitution, as in $(command arg1 arg2).

Shellcheck is a great tool for checking the syntax of shell scripts. When fed your script it says:

SC2154: date is referenced but not assigned (for output from commands, use "$(date ...)").

Bad substitution error in bash script

Try using:

#!/bin/bash

instead of

#! /bin/sh

bash : Bad Substitution on array variable

${#runtime_args[@]} is number of elements in the array. The trialing - is what triggers the error. Is this script generated? It looks like a syntax error to me.



Related Topics



Leave a reply



Submit