24.5. Shells, Scripting, Programming, and Compiling
24.5.1. Objective 1: Customize and Use the Shell Environment
A shell presents an interactive Textual User Interface, an operating environment, a facility for launching programs, and a programming language.
Shells can generally be divided into those derived from the Bourne shell, sh (including bash), and the C-shells, such as tcsh.
Shells are distinct from the kernel and run as user programs.
Shells can be customized by manipulating variables.
Shells use configuration files at startup.
Shells pass environment variables to child processes, including other shells.
24.5.1.1. bash
bash is a descendant of sh.
Shell variables are known only to the local shell and are not passed on to other processes.
Environment variables are passed on to other processes.
A shell variable is made an environment variable when it is exported.
This sets a shell variable:
# PI=3.14
This turns it into an environment variable:
# export PI
This definition does both at the same time:
# export PI=3.14
Shell aliases conveniently create new commands or modify existing commands:
# alias more='less'
Functions are defined for and called in scripts. This line creates a function named lsps:
# lsps ( ) { ls -l; ps; }
bash configuration files control the shell's behavior. Table 17-1 contains a list of these files.
24.5.2. Objective 2: Customize or Write Simple Scripts
Scripts are executable text files containing commands.
Scripts must have appropriate execution bits set in the mode.
Some scripts define the interpreter using the #!/bin/bash syntax on the first line.
24.5.2.1. Environment
A script that starts using #!/bin/bash operates in a new invocation of the shell. This shell first executes standard system and user startup scripts. It also inherits exported variables from the parent shell.
Like binary programs, scripts can offer a return value after execution.
Scripts use file tests to examine and check for specific information on files.
Scripts can use command substitution to utilize the result of an external command.
Scripts often send email to notify administrators of errors or status.