Summary
Variables are necessary for any serious programming. Shell
variables hold string values, and a large array of operators for use in
${
var...}
lets you control the results of variable substitution.
The shell provides a number of special variables (those with
nonalphanumeric names, such as $? and
$!), that give you access to special
information, such as command exit status. The shell also has a number of
special variables with predefined meanings, such as PS1, the primary prompt string. The positional
parameters and special variables $*
and $@ give you access to the
arguments used when a script (or function) was invoked. env, export, and readonly give you control over the
environment.
Arithmetic expansion with $((...))
provides full arithmetic capabilities, using the same operators and
precedence as in C.
A program's exit status is a small integer number that is made
available to the invoker when the program is done. Shell scripts use the
exit command for this, and shell
functions use the return command. A
shell script can get the exit status of the last command executed in the
special variable $?.
The exit status is used for control-flow with the if, while,
and until statements, and the
!, && and || operators.
The test command, and its alias
[...], test file attributes and string and numeric
values, and are useful in if,
while, and until statements.
The for loop provides a
mechanism for looping over a supplied set of values, be they strings,
filenames, or whatever else. while
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access