6

I have a bash script that is run nightly in a cron job. It needs to do case insensitive file matching, so the script calls

shopt -s nocaseglob

I want to make sure this does not affect other cron scripts. Does this setting persist after this one script ends? Or is this setting enabled only for the duration of the script?

Thanks!

Jeff
  • 163

1 Answers1

7

Setting options with shopt is a shell setting. It only affects the shell instance that you run it in: it is local to the shell process and to subshells invoked by $(…), (…) and similar constructs. It has no effect on other shell scripts executed concurrently or later, nor even on independent bash scripts that happen to be executed from commands executed by this script.

The same applies to the values and types of variables, as long as they aren't exported. It's also possible to have variables that are local to a function; options are always global, in the sense that if you set them in a function, they remain in place when the function returns.

Environment variables (i.e. exported variables), I/O redirections, resource limits, umask, current directory and a number of other settings apply to the current shell process as well as to all subprocesses (i.e. all commands invoked by that script). They too do not escape to unrelated processes that may be executed concurrently.

  • Suppose I want to enable a commonly used shopt option like globstar persistently, would the best way to do that be simply adding shopt -s globstar to my .bash_profile? – Taylor D. Edmiston Feb 14 '19 at 16:48
  • 2
    @TaylorEdmiston No, .bash_profile only takes effect in login shells. If you want it to apply to all interactive shells, put this in .bashrc. See https://unix.stackexchange.com/questions/122187/is-there-a-bash-file-that-will-be-always-sourced-in-interactive-mode-no-matter-i/122188#122188 If you want it to apply in a script, it needs to be in the script itself. – Gilles 'SO- stop being evil' Feb 14 '19 at 17:47