Deepak
Deepak

Reputation: 43

Shell script - Bad substitution Error while running script with sh

I am trying to loop inside a directory and rename the file name with space. But i m getting the bad substitution error while running it with sh test.sh

#!/bin/bash
for f in /home/admin1/abc/*.kmz
do
mv "$f" "${f// /_}"
#rm $i
done

Since i need to configure in crontab i may need to run it with sh command not with ./

Upvotes: 0

Views: 12093

Answers (2)

AKX
AKX

Reputation: 169051

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.

Upvotes: 5

yacc
yacc

Reputation: 3361

Bourne Shell sh doesn't support this type of substitution. You could run this script:

for f in /home/admin1/abc/*.kmz
do
  mv "$f" `echo "$f" |tr ' ' _`
done

Upvotes: 1

Related Questions