7

I have the following bash script,

#!/bin/bash
echo $SHELL

i=1
for img in ../img/*.png; do
  echo $img
  new=$(printf "../img/tmp/pyr%d.png" "$i")
  echo $new
  # cp "$img" "$new"
  let i=i+1
done
# avconv -r 2  -i ../img/tmp/pyr%d.png -b:v 1000k ../pop_pyr.mp4

In the ../img folder, there are files pyr1920.png, pyr1925.png, ...

When I run this script, I get this error

../img/pyr1920.png
../img/tmp/pyr1.png
make_video.sh: 10: make_video.sh: let: not found
../img/pyr1925.png
../img/tmp/pyr1.png

But I pasted the script in a terminal, it works perfectly fine.

../img/pyr1920.png
../img/tmp/pyr1.png
../img/pyr1925.png
../img/tmp/pyr2.png

My environment it ubuntu 14.04. I understand that default /bin/sh is dash. I make sure to use bash and check by echo $SHELL.

Why it doesn't work as a script but works fine from a terminal? I tried to find a solution but all I can find is about "dash" and "bash" setting.

6
  • 1
    That's not a bash error message; you aren't running your script with bash. Commented Feb 22, 2017 at 13:52
  • Thank you for the info, I will check and try them out. Commented Feb 22, 2017 at 13:54
  • 1
    @chepner, Yes, now I realized it works with "bash myscript.sh". Doesn't it run bash if I write "#!/bin/bash" on the top of the script? Commented Feb 22, 2017 at 13:57
  • 2
    It should; how are you running it? Incidentally, $SHELL does not contain the name of the current shell; it's the name of your default shell. Commented Feb 22, 2017 at 14:00
  • Even my default shell is /bin/bash, I checked just open a terminal and type $SHELL. I am confused. Commented Feb 22, 2017 at 14:07

3 Answers 3

16

do in this way:

i=$((i+1))

or

i=$(expr $i + 1)
Sign up to request clarification or add additional context in comments.

1 Comment

If $((...)) doesn't work, there's no reason to believe expr is available either.
7

I am sorry for answering my question. I was running my script with sh script.sh not bash script.sh. bash script.sh worked fine.

But isn't that I can specify it by #!/bin/bash on the top of a script?

4 Comments

If you run the script by passing its name as an argument to a specific interpreter (like sh), the shebang is ignored. It is only used when you make the script executable and run it directly (chmod +x script.sh; ./script.sh)
Thank you very much for the comment! It explained clearly and saved me a lot of time. I was stucked in this for long time.
@user26767: please accept an answer to prevent others spending time on this.
BTW, adding (and accepting if you like) your own answer is NOT a bad thing. We're working to accumulate knowledge, and that's part of the process.
0

I also had the same error, checking I found that with this code you get what you need

i=`expr $i + 1`

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.