Skip to content
Advertisement

Use of $- in shell script

What is use of $- in Unix. In My system, the output is,

$ echo $-
himBH
$

what is meant by himBH? what it stands for.

Thanks in Advance…

Advertisement

Answer

It returns the current shell’s flags. The $- variable contains the shell’s flags currently active in your terminal. These flags determine how your shell will function for you. In your case, these flags have the following meaning (note that all of these are defaults):

  • h: Short for “hashall” (read that “hash all”), this tells bash to remember the locations of commands it has found through querying your PATH.

  • i: Short for “interactive”, which is good, because this is a shell with which I’m interacting (entering input & getting back output)!1

  • m: Short for “monitor”, this enables job control in bash (so you can send jobs to the background via bg, for instance).

  • B: Short for “braceexpand” (read as “brace expand”), this allows you to use the very efficient brace expansion in bash2.

  • H: Short for “histexpand” (read as “hist [history] expand”), this is what enables you to re-run a command from your history by prefacing its number with an exclamation point3.

These flags can be changed by using the set command. For example :

set +H 
echo $- 
himB

remove the H flag. Whereas -

set -H
echo $- 
himBH

restores it. That’s it, a bit confusing but + removes a flag, whereas - adds a flag.


More info:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement