Ubuntu – What does “[[ $- != *i* ]] && return” mean

bashrccommand line

I have this line in my .bashrc and I would like to know what exactly this means

# If not running interactively, don't do anything
[[ $- != *i* ]] && return

Best Answer

  • $- means 'current flags'.
  • echo $- returns "himBH". Those are all defaults.
  • so ... [[ $- != *i* ]] && return actually does what it says above in a comment: it checks if the interactive flag is set. The [[ and ]] make it a boolean so it ends up in a "true" or "false". "false && return" makes it go on "true && return" makes it execute the return.

The default flags explained in more detail:

h is for "hash all": this tells bash to remember the locations of commands it has found through querying your PATH.

i is for "interactive": entering input & getting back output.

m is for "monitor": this enables job control

B is for "brace expand". This allows you to use brace expansion

H is for "history expand". This is what enables you to rerun a command from your history by prefacing its number with an exclamation point


By the way. I have ...

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

Basically does the same but easier to read I assume.