0

I am trying to write shell script (sh), Where I am getting below error

variable i contains:

 test.txt

code:

 echo "${i/.txt/}"

Error:

 just.sh: 16: just.sh: Bad substitution

expected output string :

 text

Reproduce steps

Create file:

 touch text.txt

Create file test.sh contents using any of editor

code:

#!/bin/sh
for i in `find *.txt`
do
    echo "$i"
    echo "${i/.txt/}"
done

How to run:

 sh test.sh 
asteroid4u
  • 125
  • 1
  • 12

1 Answers1

1

sh is not bash. Fix your shebang (the 1st line) as #!/bin/bash first.

References

  • Difference between sh and bash, search "expansion" in the thread

    Bash features a rich set of expanded non-standard parameter expansions such as ${substring:1:2}, ${variable/pattern/replacement}, case conversion, etc.

Simba
  • 23,537
  • 7
  • 64
  • 76