Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do the characters in the bash environment variable $- mean?

Tags:

linux

bash

I've been looking through some of the .bashrc and .profile scripts that come with various Linux distros and am seeing that sometimes they check $-.

Here's one in Ubuntu

case $- in
    *i*) ;;
    *) return;;
esac

In this case it's checking for the "i" flag is present to see if the current shell is an interactive one.

My current session gives me this :

# echo $-
himBH

What are the other flags/options mean? Is there a comprehensive list somewhere?

like image 555
peonicles Avatar asked Aug 09 '13 12:08

peonicles


People also ask

What does $? Mean in bash script?

$? $0 is one of the most used bash parameters and used to get the exit status of the most recently executed command in the foreground. By using this you can check whether your bash script is completed successfully or not.

What is the _ environment variable in Linux?

In Linux and Unix based systems environment variables are a set of dynamic named values, stored within the system that are used by applications launched in shells or subshells. In simple words, an environment variable is a variable with a name and an associated value.

What are environment variables in bash?

In Bash, environment variables define many different things : your default editor, your current username or the current timezone. Most importantly, environment variables can be used in Bash scripts in order to modify the behaviour of your scripts.

What characters are allowed in environment variables?

Only uppercase letters, lowercase letters, and underscores from this character set are allowed.


2 Answers

From man bash:

-

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).

So these are the current options that control the behavior of the shell. In particular:

  • h: Cache location of binaries in the $PATH. Speeds up execution, but fails if you move binaries around during the shell session.
  • i: The current shell is interactive
  • m: Job control is enabled
  • B: Brace expansion is enabled
  • H: History substitution like !-1
like image 192
phihag Avatar answered Oct 28 '22 21:10

phihag


They mean various things. Each letter corresponds to an option being set for bash. eg, "i" means that the shell is interactive (so the code sample you gave is a test to see if it's an interactive shell or not).

A full list is available in the bash man page. Look for "set" - here's the first line:

set [+abefhkmnptuvxBCEHPT] [+o option-name] [arg ...]
like image 30
pgl Avatar answered Oct 28 '22 21:10

pgl