2

I am searching for the initial setting of the $- variable. From which file and at which point it gets initialized?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
l00p
  • 263
  • 1
  • 3
  • 11

1 Answers1

4

From which file and at which point it gets initialized?

It depends on how the shell was invoked, and there may be set command invocations in configuration files such as /etc/bash.bashrc or /etc/profile and even ~/.bashrc. So there's no one file for initializing it.

To quote bash manual:

- Expands to the current option flags as specified upon invocation, by the set builtin command, or those set by the shell itself (such as the -i option).

In other words, some of the options depend on how the shell was invoked (login shell or interactive shell), some options are set by the shell itself (that is, they're default options), and then there's manually set ones. The set built-in part is simple - that's in your control, for example

$ echo $-
himBHs
$ set -b
$ echo $-
bhimBHs

or it can be used in a configuration file that the shell will read.

You might want to refer to Difference between Login Shell and Non-Login Shell? and Differentiate Interactive login and non-interactive non-login shell. These two posts cover greatly differences between how shell is invoked,but the later post by terdon shows a good example of options set for login and non-login shell. At the same time, you may be wondering how does the shell know the way it's invoked to set appropriate options ? Well, there's a post for that: How does bash know how it is being invoked?:

Normally bash knows that it's a login shell because when the login program invokes it, it tells bash that its name is -bash. That name is in argv[0], the zeroth command line argument

As mentioned, some options are default. For example,

-m Monitor mode. Job control is enabled. This option is on by default for interactive shells on systems that support it -h Remember the location of commands as they are looked up for execution. This is enabled by default.

So as mentioned before, the shell can look at how it was invoked and enable or not -m, for example.