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
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.Sign up to join this community
$-means 'current flags'.
echo $-returns "himBH". Those are all defaults.
[[ $- != *i* ]] && returnactually 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.
Basically it means "If the shell flags don't contain a lowercase i, then stop the function/script here".
denotes a conditional expression in bash
means the shell flags. It will contain "i" if the shell is interactive (ie. you can type commands, as opposed to, it's executing a script)
simply means "not equal to".
is a wildcard pattern that matches anything containing "i"
without any arguments just means stop the execution of the current function or script.
is the logical AND operator. The expression at the right, in this case
return, will only be evaluated (executed) if the expression at the left is true. So in many programming languages,
a && b is a shorthand for
if a then b -- this is called "short-circuit evaluation" and it drives programming teachers crazy when you use it :D