Sign in to save

Bookmark this page so you can find it later.

Sign in to save

Bookmark this page so you can find it later.

Bash scripting uses shell commands to automate tasks, manage files, and run programs from the command line. This cheat sheet helps students remember the syntax patterns that appear again and again in scripts. It is useful for writing small automation tools, processing text files, and understanding how operating systems handle commands.

Clear examples make it easier to avoid common syntax errors.

Key Facts

  • A Bash script usually starts with the shebang #!/bin/bash so the system knows to run the file with Bash.
  • A variable is assigned with name=value and used with $name, with no spaces around the equals sign.
  • Command-line arguments are read with 1,1, 2, 3,andthetotalnumberofargumentsisstoredin3, and the total number of arguments is stored in #.
  • An if statement uses if [ condition ]; then commands; fi, and spaces are required inside the square brackets.
  • A for loop can repeat through values using for item in list; do commands; done.
  • A while loop repeats while a condition is true using while [ condition ]; do commands; done.
  • Input and output redirection use > to overwrite a file, >> to append to a file, and < to read input from a file.
  • A pipe sends the output of one command into another command using command1 | command2.

Vocabulary

Shell
A shell is a command interpreter that lets a user run programs and control the operating system.
Bash
Bash is a common Unix shell and scripting language used to run commands and automate tasks.
Shebang
A shebang is the first line of a script, such as #!/bin/bash, that tells the system which interpreter to use.
Variable
A variable is a named storage location that holds text, numbers, or command results for later use in a script.
Pipe
A pipe is the | operator that sends the output of one command as input to another command.
Redirection
Redirection changes where a command gets input from or sends output to, such as a file instead of the screen.

Common Mistakes to Avoid

  • Writing spaces around variable assignment, such as count = 5, is wrong because Bash treats count as a command instead of creating a variable.
  • Forgetting spaces in test brackets, such as [ $x -gt 5], is wrong because Bash requires spaces after [ and before ].
  • Using > when you meant >> is wrong because > overwrites the file, while >> adds new output to the end of the file.
  • Forgetting to quote variables, such as using nameinsteadof"name instead of "name", can break scripts when the value contains spaces or special characters.
  • Running a script without execute permission is wrong because the operating system may refuse to run it until chmod +x script.sh is used.

Practice Questions

  1. 1 Write a Bash command that creates a variable named score with the value 92, then prints The score is 92.
  2. 2 Write an if statement that prints Pass if a variable named grade is greater than or equal to 70.
  3. 3 Write a pipeline that counts how many lines in students.txt contain the word absent.
  4. 4 Explain why pipes and redirection are useful when building a script that processes a large text file.

Understanding Bash Scripting Reference

Bash works by reading a line, expanding parts of that line, then running a command. Expansion is a major source of surprises. A variable reference is replaced by its value before the command starts.

Wildcards can expand into matching filenames. Text inside double quotes still allows variable expansion, while text inside single quotes is treated literally.

Quoting variable values is a good habit, especially when a filename may contain spaces. Without quotes, one filename with a space can be split into two separate arguments.

Most commands report success or failure through an exit status. Zero usually means success. A nonzero value means that something went wrong or that a test was false.

Conditional statements use this result. Commands such as grep are useful examples because grep succeeds when it finds matching text and fails when it finds none. This makes scripts able to respond to real conditions rather than blindly continue.

Students should learn to distinguish a command that prints output from a command that reports its status. They are related, but they are not the same thing.

Files are handled as streams of bytes flowing into or out of programs. Standard input is the usual incoming stream, often typed at a keyboard. Standard output is the normal result stream shown in the terminal.

Standard error carries warnings and error messages. Redirection changes where these streams go. This matters when a script creates a report, saves a list of results, or keeps errors separate from normal output.

Appending is safer than overwriting when a script records repeated measurements or log entries. Before redirecting output, check the target filename carefully because overwriting can destroy existing work.

Pipes make small commands work together. One program can select lines, another can sort them, and a final program can count or display the result. Common text tools include grep for finding patterns, sort for ordering lines, uniq for grouping repeated adjacent lines, wc for counting, and cut for selecting fields from structured text.

These tools appear in school tasks such as checking survey responses, summarizing marks stored in a text file, or finding repeated entries in a data list. A pipeline is easier to understand when each stage has one clear job.

Reliable scripts need careful handling of filenames, user input, and repeated work. Use meaningful variable names so the purpose of each value is clear. Test scripts on copies of files, not important originals.

Print short progress messages when a longer task runs. Loops should have a clear stopping point, since a while loop with a condition that never changes can run forever.

Indent the commands inside conditions and loops even though Bash does not require it. Good formatting helps people spot missing words, misplaced brackets, and commands that run in the wrong part of a script.