19

I'm writing a script to copy some files around, and trying to use shopt -s dotglob to enable cp to copy dotfiles like .jshint and whatnot.

I can run shopt -s dotglob directly at a bash prompt with no error. However, running the script throws the error:

script.sh: 81: script.sh: shopt: not found

I'm running this script in bash shell, with the shebang header #!/usr/bin/env bash. Error line:

shopt -s dotglob
cp -r $TEMP/img/* $TARGET/img/
cp -r $TEMP/js/* $TARGET/js/
cp -r $TEMP/less/* $TARGET/less/

Not finding anything helpful on google, any idea what the problem is here?

4
  • Thanks for the reminder, selected answers for all that I could. Still one besides this question that doesn't have a good answer yet.
    – bgibson
    Aug 27, 2012 at 8:15
  • 3
    Try with the simpler #!/bin/bash header?
    – ish
    Aug 27, 2012 at 8:17
  • And which Ubuntu version?
    – ish
    Aug 27, 2012 at 8:19
  • 2
    @izx, that is the right answer, shopt is a bash builtin, sh does not have shopt, and the error message looks like an error message from dash. So most likely the error here is running a bash script with sh (which in Ubuntu is dash by default). Even if sh is a symlink to bash, running a bash script with sh is not the same as running it with bash.
    – geirha
    Aug 27, 2012 at 10:25

2 Answers 2

28

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.

3
  • 7
    To be completely cross-system, I prefer: #!/usr/bin/env bash as it is env's job to know which bash to use (in case you've patched it, for example).
    – shrikeh
    Jan 25, 2014 at 14:16
  • Exactly what I looking for. Thx :)
    – Meow Kim
    Jun 23, 2023 at 1:21
  • Just adding in something here for the newbies or those (like me) that constantly get slapped in the face by minutiae errors... "#!/bin/bash" really does need to be on the first line. My script somehow got a newline put in for line one so the shell declaration was on line 2. I've been punching dirt for two days trying to figure out why my perfectly fine script suddenly stopped working.
    – breusshe
    Aug 2, 2023 at 15:16
3

You need to exit zsh and activate bash as shown:

exec bash

The run the command

source ~/.bashrc

After which you can reactive zsh:

exec zsh

Hope that helps

4
  • Hmmm. 1) I don't think the OP was using zsh, 2) AFAIK ~/.basrc is run by bash on startup in this case so there is no need to call it explicitly, 3) where is the OP's command executed? and 4) using exec twice makes you lose any environment changes you have done in the initial zsh shell, which wouldn't happen if you would just call bash.
    – xenoid
    Oct 29, 2019 at 10:27
  • In my case, it was needed to pick up environment changes and the changes did persist. I just restarted the computer and all my changes are persisted. Maybe it is just specific to anaconda which I was trying to setup though Oct 29, 2019 at 11:57
  • i am not sure why this answer is down voted, this is also one of the way, upvoting
    – Ravi
    Jun 7, 2020 at 16:04
  • the problem is some command is only exist in the bash environment if you usually use linux command
    – Yohanes AI
    May 20, 2023 at 9:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .