4

The first thing in my bashrc file is this expression:

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

Can somebody explain what this means?

all these symbols make it really hard to google, and there is no Haskell "hoogle" equivalent for bash so I can search for symbol expressions.

The intended behavior seems to be similar to this.

nonsourced=0;

# if sourced,
if [[ "$0" == "$BASH_SOURCE" ]]; then
    nonsourced=1;
else
    nonsourced=0;
fi

echo "nonsourced? $nonsourced";


case $- in
    *i*) 
        # this case is entered if "$-" contains "i".
        if [[ "$nonsourced" -eq "0" ]]; then
            echo "1. " "$-";
        fi
        ;; # leave case?
    *) # this case is entered in all other cases.
        if [[ "$nonsourced" -eq "0" ]]; then
            echo "2. " "$-";
            return
        else
            # cannot return from nonsourced, use exit.
            echo "avoided return from nonsourced #2";
            exit 0;
        fi
        ;; # leave case?
esac

echo "3";
Dmytro
  • 5,068
  • 4
  • 39
  • 50
  • `*)` is the "default", an interesting link: http://www.linux.org/threads/case-statement-in-bash-scripts.5602/ (I searched "bash case asterisk") –  Nov 22 '16 at 17:03
  • You can try symbolhound.com for searching with strings that contain symbols. – meatspace Nov 22 '16 at 17:07
  • Why do you need to google? Don't you know where the bash manual is located? http://www.gnu.org/software/bash/manual/html_node/index.html – Barmar Nov 22 '16 at 17:08
  • Isn't it invalid to use return from outside a function? This part of code is outside a function; it might be sourced, but assuming this is wrong. – Dmytro Nov 22 '16 at 17:12
  • You don't need to google anything. Type `man bash` in your shell, keep calm and start reading. Everything is explained there. – axiac Nov 22 '16 at 21:51

3 Answers3

4

Can somebody explain what this means?

  • $- The list of options set in the shell at the point of evaluation.

    When a shell (bash) starts it accepts some options:

    LESS=+'/^ *OPTIONS' man bash

    All of the single-character shell options documented in the description of the set builtin command can be used as options when the shell is invoked. In addition, bash interprets the following options when it is invoked:

    One of such options is -i. So calling bash as bash -i … should [a] trigger that option inside[a] the shell.

    [a] I say should as some other conditions are also required to have an effective interactive shell. Also, an interactive shell may be started by simply writing bash in a terminal (no -i option used)

    [b] The way to print some options that have been set is with echo $-

  • *i*) ;; tests if the string from $- contains an i, if so, do nothing.

  • *) return;; On any other value of $- return (go out the script[c] ).

    [c] Please read this answer for return vs. exit.

In overall, it does what the comment says:

# If not running interactively, don't do anything

Or with a clearer wording:

# If running interactively, exit[d].

[d] It may be more technically correct to use the word return instead of exit, but the idea is cleaner, I believe.

Note that there is a quite similar construct with $PS1 (used in /etc/bash.bashrc and repeated in ~/.bashrc in debian based systems, for example):

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

As for the problem of finding symbols:

> all these symbols make it really hard to google

even if it doesn't cover so many pages, SymbolHound may be of help here.

If we try it

we find this

Which clearly explains what you are asking.

Community
  • 1
  • 1
2

See the documentation for Bash variables:

$-: A hyphen expands to the current option flags as specified upon invocation, by the set built-in command, or those set by the shell itself (such as the -i).

The asterisks in the case patterns are wildcards, so essentially the whole case says “if there’s an i [as interactive] somewhere in the arguments, go on, otherwise return”.

zoul
  • 102,279
  • 44
  • 260
  • 354
  • how are $- options set/unset? they are not arguments since `echo $-;` script always says hB. – Dmytro Nov 22 '16 at 17:15
  • I think it’s created internally by Bash, that’s probably also why it’s safe to match for the `i` in such a simple way. I tried to find the right part in the source code, but didn’t succeed. – zoul Nov 22 '16 at 17:33
  • The return works because it's assuming it is sourced right? return can't work outside sourced contexts/functions and it's not wrapped in a function. – Dmytro Nov 22 '16 at 17:52
0

$- lists the current shell options.

The two cases are whether the -i interactive flag is present anywhere in that list of options.

meatspace
  • 859
  • 16
  • 25