Bash Built-In Variables: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 12: Line 12:


==Environment Variable Hierarchy==
==Environment Variable Hierarchy==
When a parent shell forks a new sub-shell, the sub-shell process has ''a copy of the parent's environment'' and no access to the parent process's environment whatsoever. When the shell sub-process terminates any changes made to its environment are lost. There is no way to modify directly the parent's environment.


=Standard Environment Variables=
=Standard Environment Variables=

Revision as of 17:04, 24 May 2017

External

Internal

Overview

Environment Variable Hierarchy

Standard Environment Variables

IFS

IFS is the internal field separator. This variable determines how bash recognizes fields (word boundaries) when it interprets character strings. IFS defaults to whitespace (space, tab and newline). This is the proof:

echo "$IFS" | cat -vte
 ^I$
$

IFS can be changed. For example, this is how you set IFS so for iterates over lines:

IFS="$(printf '\n\r')"
Note you must set IFS back to whitespace after setting it to something else, so the basic shell function work as expected. This is done as shown below: restoring the default IFS value.

Restoring the default IFS value

IFS="$(printf ' \t\n')"

IFS and for

for honors the value of IFS (default the space). If you set IFS to something else, before the for statement, for will use that as field separator while iterating over the list.

Note be extremely careful when setting IFS before a for loop, even if you restore the default value after the loop: everything inside the loop will use the non-standard IFS value and it may not work as expected.

Also see:

$*

All arguments on command line as one string ("$1 $2 ..."). The values are separated by the first character in $IFS.

Also see:

bash set and Positional Parameters

$@

All arguments on command line, individually quoted ("$1" "$2" ...).

$?

Exit value of last executed command, in the interactive shell or within a script.

After the execution of a command in the context of an if, the exit code can be retrieved with $?:

if ! bash -c "exit 4"; then
    echo "we expect the failure code to be 4 and it is $?"
fi

$$

The pid of the current process. Also see $BASHPID, which differs from $$ under certain circumstances, such as subshells that do not require bash to be re-initialized.

$PPID

The process ID of the shell's parent. This variable is readonly.

$#

Number of command line arguments

$-

Options currently in effect (supplied on command line or to set). The shell sets some options automatically.

$

Process number of the shell.

$!

Process number (PID) of last background command.

$0

First word; that is, the command name. This will have the full pathname if it was found via a PATH search.

$1, $2, ...

Individual arguments on command line (positional parameters). The Bourne shell allows only nine parameters to be referenced directly (n = 1–9); Bash allows n to be greater than 9 if specified as ${n}.

$_

Temporary variable; initialized to pathname of script or program being executed. Later, stores the last argument of previous command. Also stores name of matching MAIL file during mail checks.