shopt works in command line, not found when run in a script
To form an answer from the comments:
Many people out of habit run their scripts with sh
instead of bash
. This is a good practice if portability is a concern, but many people do so because they're copying something they've seen without understanding it.
Unless your script needs to run on a non-desktop Linux system (e.g., running shell scripts on Android devices is quite different), I recommend using the Bash shebang line at the beginning:
#!/bin/bash
This line, when it's the first line in the script, determines which interpreter (shell such as bash or sh, Python, etc.) is called to execute it. If you use the above line, you'll get the same behavior (almost) as you do from the command line, assuming you use the default shell. If for reasons of portability or preference you use a different shebang line, be aware that you'll have to consult the documentation for the shell you've referenced, even if the shell you reference is a symlink to Bash.