4

I find that the default shell that I use when I start Terminal (with Ctrl+Alt+T) is /bin/bash.

And running the built-in command type on bash as:

type -p bash

returns the result as:

/bin/bash

Suggesting the same executable is run when I do Ctrl+Alt+T or say bash.

This shell is the same as an invocation of the command bash with some of its options passed to it right? If yes, what are these options?

As an example, perhaps the -i option gets passed so as to make the bash shell interactive?

PS: Maybe this is related too, but I cannot seem to piece together all the pieces here.

1 Answers1

8

Usually, no, the terminal does not pass any options to bash. bash does, however, assume a set of default options depending on how it was invoked. From man bash, section INVOCATION:

An interactive shell is one started without  non-option  arguments  and
without the -c option whose standard input and error are both connected
to terminals (as determined by isatty(3)), or one started with  the  -i
option.   PS1 is set and $- includes i if bash is interactive, allowing
a shell script or a startup file to test this state.

An interactive shell further activates other options. Further some defaults apply based on the invocation name (sh vs bash). Reading on (section on set):

-h      Remember  the location of commands as they are looked up
        for execution.  This is enabled by default.
-m      Monitor mode.  Job control is enabled.  This  option  is
        on  by  default  for  interactive shells on systems that
        support it (see JOB CONTROL above)
-B      The  shell performs brace expansion (see Brace Expansion
        above).  This is on by default.
-H      Enable !  style history substitution.  This option is on
        by default when the shell is interactive.

Combined, simply invoking bash on a terminal will enable these options.

You can confirm this by checking the value of the $- special variable:

$ echo $-
himBH

An additional option that maybe set is if your terminal is set to start login shells. In that case -l is explicitly passed by the terminal as an option.

muru
  • 197,895
  • 55
  • 485
  • 740
  • So when I say Ctrl+Alt+T I start the gnome terminal emulator which then launches my default shell (here, bash) only with the options which I see when I say echo $-? Also, what exactly is $-? Links to some kind of documentation, article etc will be appreciated. – Slothworks Aug 13 '15 at 11:32
  • 1
    @Slothworks $- is a variable which contains the current set of options (search for $- in the manpage). Technically, GNOME Terminal doesn't launch your shell with any options at all. But the state in which it starts (connected to a terminal, not processing a file or executing a command) makes bash assume a set of default options - the one listed above. For example, if you open a terminal and run bash manually, it would also have these options, even though you did not use any options. – muru Aug 13 '15 at 11:36
  • But if I were to say bash <some script> it could have a different set of parameters? Like you said, because of the way it was invoked. Is that right? – Slothworks Aug 13 '15 at 11:47
  • 1
    @Slothworks yes. It's likely it will only have hB. And ignore my previous comment. I meant to say: compare with the output of bash -c 'echo $-' – muru Aug 13 '15 at 11:53
  • Ah yes. Running bash -c 'echo $-' says hBc which is lot more enlightening. Also, on a side note (probably this is best asked as a separate question too) , why is it that a script that does not have execute permissions gets executed when run as bash <script>? A brief answer is present here, but some more background and a little more elaboration would be nice. – Slothworks Aug 13 '15 at 12:03
  • @Slothworks well, technically the script isn't being executed, bash is. The system calls involved (see man 3 exec) care about the permissions and type of the file being executed (bash when you do bash foo, ./foo when you do ./foo), and not what the arguments are. It's bash who will worry about whether the arguments are valid or not. – muru Aug 13 '15 at 12:17
  • All the active options are stored in $SHELLOPTS (set-builtin-options) and $BASHOPTS (shopt-builtin-options). There are a few more than mentioned above...some are manually beeing set in ~/.bashrc, the others are active by default. 'checkwinsize' can be deleted in ~/.bahrc since it is active by default...execute 'help set' & 'help shopt' to check their meanings. – Neni Aug 26 '16 at 06:05