Book: LPI Linux Certification in a Nutshell
Section: Chapter 24.  Exam 102 Highlighter's Index



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 24-3 contains a list of these files.

Table 24-3. Bash Configuration Files

File

Description

/etc/profile

The system-wide initialization file; executed when you log in.

/etc/bashrc

Another system-wide initialization file; may be executed by a user's .bashrc for each bash shell launched.

~/.bash_profile

If this file exists, it is executed automatically after /etc/profile when you log in.

~/.bash_login

If .bash_profile doesn't exist, this file is executed automatically when you log in.

~/.profile

If neither .bash_profile nor .bash_login exists, this file is executed automatically when you log in.

~/.bashrc

This file is executed automatically when bash starts.

~/.bash_logout

This file is executed automatically when you log out.

~/.inputrc

This file contains optional key bindings and variables that affect how bash responds to your keystrokes.

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.

  • Refer to Chapter 17 for details on bash commands.