Chapter 2. Unix Shell Scripting

Table of Contents

2.1. What is a Shell Script?
2.1.1. Self-test
2.2. Scripts vs Programs
2.2.1. Self-test
2.3. Why Write Shell Scripts?
2.3.1. Efficiency and Accuracy
2.3.2. Documentation
2.3.3. Why Unix Shell Scripts?
2.3.4. Self-test
2.4. Which Shell?
2.4.1. Common Shells
2.4.2. Self-test
2.5. Writing and Running Shell Scripts
2.5.1. Self-test
2.6. Shell Start-up Scripts
2.6.1. Self-test
2.7. Sourcing Scripts
2.7.1. Self-test
2.8. Scripting Constructs
2.9. Strings
2.10. Output
2.10.1. Self-test
2.11. Shell and Environment Variables
2.11.1. Assignment Statements
2.11.2. Variable References
2.11.3. Using Variables for Code Quality
2.11.4. Output Capture
2.11.5. Self-test
2.12. Hard and Soft Quotes
2.12.1. Self-test
2.13. User Input
2.13.1. Self-test
2.14. Conditional Execution
2.14.1. Command Exit Status
2.14.2. If-then-else Statements
2.14.3. Conditional Operators
2.14.4. Case and Switch Statements
2.14.5. Self-test
2.15. Loops
2.15.1. For and Foreach
2.15.2. While Loops
2.15.3. Self-test
2.16. Generalizing Your Code
2.16.1. Hard-coding: Failure to Generalize
2.16.2. Generalizing with User Input
2.16.3. Generalizing with Command-line Arguments
2.16.4. Self-test
2.17. Scripting an Analysis Pipeline
2.17.1. What's an Analysis Pipeline?
2.17.2. Where do Pipelines Come From?
2.17.3. Implementing Your Own Pipeline
2.17.4. An Example Genomics Pipeline
2.18. Functions and Calling other Scripts
2.18.1. Bourne Shell Functions
2.18.2. C Shell Separate Scripts
2.18.3. Self-test
2.19. Alias
2.20. Shell Flags and Variables
2.21. Arrays
2.22. Good and Bad Practices
2.23. Here Documents
2.24. Common Unix Tools Used in Scripts
2.24.1. Grep
2.24.2. Stream Editors
2.24.3. Tabular Data Tools
2.24.4. Sort/Uniq
2.24.5. Perl, Python, and other Scripting Languages

Before You Begin

Before reading this chapter, you should be familiar with basic Unix concepts (Chapter 1, Using Unix) and the Unix shell (Section 1.4.3, “Command Line Interfaces (CLIs): Unix Shells”).

2.1. What is a Shell Script?

A shell script is essentially a file containing a sequence of Unix commands. A script is a type of program, but is distinguished from other programs in that it represents programming at a higher level.

While a typical program is largely made of calls to subprograms, a script contains invocations of whole programs.

In other words, a script is a way of automating the execution of multiple separate programs in sequence.

The Unix command-line structure was designed to be convenient for both interactive use and for programming in scripts. Running a Unix command is much like calling a subprogram. The difference is just syntax. A subprogram call in C encloses the arguments in parenthesis and separates them with commas:

function_name(arg1,arg2,arg3);
	

A Unix command is basically the same, except that it uses spaces instead of parenthesis and commas:

command_name arg1 arg2 arg3
	

2.1.1. Self-test

  1. What is a shell script?
  2. How are Unix commands similar to and different from subprogram calls in a language like C?