2

I have two statements to return from ~/.bashrc if the shell is not interactive, probably from two Ubuntu versions, but I can't reconstruct which one is newer (and I assume thus better) and I'm interested in the effect of the different approaches:

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

vs.

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac
  • 1
    You can write it more tersely: [[ $- == *i* ]] || return – glenn jackman Aug 29 '18 at 15:45
2

According to the Bash Hackers Wiki, the most reliable method is to check for the existance of the i letter in the variable $- (your second method).

There are cleaner ways to implement this action, like the following:

[[ $- == *i* ]] || return

For more information, check this answer on the Unix&Linux StackExchange community.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.