0

I have a perfectly functional script

for i in {1..15}
do
    amixer -D pulse sset Master 1%+;
done

But the command only runs once when i call it as ./volume.sh. If I copy and paste the code into the terminal, it runs fine. What's the difference?

ecc
  • 1,490
  • 1
  • 17
  • 42
  • I couldn't find it. I found many other edge cases. Feel free to link the duplicate if you find it. – ecc Jan 29 '19 at 13:55
  • 1
    Link added. This is also #2 in the "Before asking about problematic code" section of https://stackoverflow.com/tags/bash/info; search for the paragraph with the subheading "Make sure you run the script with `bash`, not `sh`". – Charles Duffy Jan 29 '19 at 13:58
  • To be honest, Google never takes you to the wiki. And no one uses in-site search instead of search Google... – ecc Jan 29 '19 at 13:59
  • Thanks for the link. I'm still pretty sure a bash newbie like me would never google "brace expansion". Related, yes, but not really a duplicate, is it? – ecc Jan 29 '19 at 14:02
  • The whole point of keeping duplicates around instead of deleting them is to make it easier to find the single place with the most canonical answer to a question. Duplicates that use different phrasing, like what you added here, are the kind of duplicate we *want* to keep around in our knowledgebase (flagged to link back to the existing instance). – Charles Duffy Jan 29 '19 at 14:22
  • 1
    ...point being, that you're using more newbie-friendly search terms not present in the canonical instance doesn't make this not-a-duplicate; instead, it makes it a *highly desirable* duplicate. It's very intentional that being closed-as-dupe doesn't prevent a question from receiving upvotes. – Charles Duffy Jan 29 '19 at 14:34

1 Answers1

1

I'm posting this answer because I found it very hard to find the solution anywhere else.

You need to add #!/bin/bash to the top of the script

#!/bin/bash
for i in {1..15}
do
    amixer -D pulse sset Master 1%+;
done

This header hints the terminal to run this script as bash instead of something else. It's not just a decoration.

ecc
  • 1,490
  • 1
  • 17
  • 42