1

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 ./

2
  • 1
    Your sh may not be bash. Try bash test.sh. Commented May 9, 2020 at 13:02
  • Glad I could help. Made my comment an answer you can accept :) Commented May 11, 2020 at 6:13

2 Answers 2

5

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.
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.