Linux: Exploring the Basics of Shell Scripting

Linux gives you many small and specialized commands, along with the plumbing necessary to connect these commands. Take plumbing to mean the way in which one command’s output can be used as a second command’s input. bash (short for Bourne-Again Shell) — the default shell in most Linux systems — provides this plumbing in the form of I/O redirection and pipes.

bash also includes features such as the if statement that you can use to run commands only when a specific condition is true, and the for statement that repeats commands a specified number of times. You can use these features of bash when writing programs called shell scripts — task-oriented collections of shell commands stored in a file.

You can learn how to write simple shell scripts, which are used to automate various tasks. For example, when your Linux system boots, many shell scripts stored in various subdirectories in the /etc directory (for example, /etc/init.d) perform many initialization tasks.

Like any programming language, the bash shell supports the following features:

Variables that store values, including special built-in variables for accessing command-line arguments passed to a shell script and other special values.

The capability to evaluate expressions.

Control structures that enable you to loop over several shell commands or to execute some commands conditionally.

The capability to define functions that can be called in many places within a script. bash also includes many built-in commands that you can use in any script.