Bash scripting lets you save and run a sequence of terminal commands, then add variables, decisions, loops and reusable functions when the task needs them. Start with a short script that prints a message or checks for a file; learn quoting and redirection before using scripts on important data. For quick lookup, use the free cheat sheets from Red Hat or Linuxize, and use GNU’s manual when you need an authoritative explanation of Bash behavior.
What Bash is—and what a Bash script does
Bash is both a command interpreter and a shell language. In a terminal, it reads commands and runs them; in a script, it runs those commands in sequence and provides language features for combining them. The GNU Bash Reference Manual describes Bash in terms of commands, functions, parameters, expansions, redirections and script execution.
A script is a plain-text file containing commands Bash can execute. It can save you from retyping a repeated command, or combine several steps into one routine. The examples below illustrate common syntax; they are not represented as tested code, and commands that change or delete files should be tried only with safe sample data.
A first script
Create a file named hello.sh with this content:
#!/usr/bin/env bash
printf '%sn' 'Hello, Bash!'
The first line asks the system to find Bash through the environment. The second runs Bash’s printf command to print a line. Save the file, then run it by passing its name to Bash:
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Repair Windows errors before they cause bigger problemsFix Now →#1 Best Overall
- Used Book in Good Condition
bash hello.sh
This avoids needing to change the file’s permissions just to run the example. Later, you can make a script executable and run it directly, but the details of permissions and execution can differ by system.
Variables: assign first, expand when you use them
A variable holds text or another value for use later. Assignment and expansion are different operations: assignment sets a value, while expansion substitutes the value wherever you refer to the variable.
name='Ada'
printf 'Hello, %sn' "$name"
There must be no spaces around the equals sign in an assignment. In "$name", the dollar sign requests parameter expansion: Bash replaces that expression with the variable’s value. Quoting it keeps the resulting value together as one argument, even if it contains spaces.
Rank #2
Use braces when they make the boundary of a variable name clearer, especially when adjoining text:
file='report'
printf '%sn' "${file}.txt"
Bash expands parameters in several contexts, so the surrounding syntax matters. The official Bash Reference Manual explains parameters and expansions in detail.
Quoting: control what Bash interprets
Spaces, dollar signs, asterisks and other characters can have special meaning to the shell. Quoting changes how Bash treats those characters: it can preserve text literally, keep a phrase together as one argument, or prevent some expansions. GNU’s manual covers quoting as a core part of Bash behavior.
- Single quotes preserve the enclosed text literally. They are useful for fixed text, such as
'Hello, Bash!'. - Double quotes preserve most characters literally while still allowing expansions such as
"$name". - Unquoted text can undergo word splitting and filename expansion. Avoid leaving variable expansions unquoted unless you specifically want those effects.
For example, "$filename" passes a filename containing spaces as one argument; an unquoted $filename may be split into multiple words. Quoting is not decoration—it helps prevent Bash from treating data as syntax.
Conditionals and loops: make scripts respond and repeat
Conditionals choose what to do based on a test. Loops repeat commands. These constructs make a script more useful than a fixed list of commands.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Check whether a file exists
if [[ -f "$1" ]]; then
printf 'File exists: %sn' "$1"
else
printf 'No regular file found at: %sn' "$1"
fi
$1 refers to the first argument supplied to the script. The -f test is true when that argument names a regular file. The quoted expansion keeps a path containing spaces together. This illustrates a check; it does not open or modify the file.
Rank #4
Repeat work with a loop
A for loop can process each item in a known list:
for item in alpha beta gamma; do
printf '%sn' "$item"
done
A while loop repeats while its condition is true. Use a condition that can eventually change, or the loop may continue indefinitely. The GNU manual documents Bash’s conditional and looping constructs in its control structures section.
Functions: give a reusable group of commands a name
Functions collect commands under a name so you can call the group more than once. They can make scripts easier to read by giving a meaningful name to a small operation.
greet() {
printf 'Hello, %sn' "$1"
}
greet 'Ada'
Here, the function receives its first argument as $1. Quoting it ensures the greeting remains correct if the argument contains spaces. A function is useful when it clarifies a task or avoids repeating the same sequence; it need not be introduced into a tiny script just for its own sake.
Best Value
Redirection: choose where input and output go
By default, commands commonly read input from the terminal and write output there. Redirection lets you send output to a file, append it to a file, or provide input from one. Bash treats redirections as part of the command line.
command > output.txtsends standard output to a file, replacing its previous contents.command >> output.txtappends standard output to a file.command < input.txtmakes a command read standard input from a file.
Be especially careful with >: it can overwrite an existing file. Test redirections with disposable files before pointing them at anything you need to keep. Bash also supports redirecting error output and combining streams; consult the manual when you need that behavior.
A practical beginner learning path
- Understand the shell and script. Run a few commands in a terminal, then put a short sequence in a text file and run it with
bash filename.sh. - Learn assignment and expansion. Set a variable without spaces around
=, then use$nameor${name}where its value is needed. - Practice quoting. Try a path with spaces and keep variable expansions double-quoted. Learn when single quotes are appropriate for literal text.
- Add decisions and repetition. Write a small
iftest and a short loop; understand the test condition before using the script on real files. - Group repeated work in functions. Name a small action and pass it arguments when that improves clarity.
- Control input and output. Try output redirection with a temporary file and confirm what
>and>>do before using them elsewhere.
Free Bash cheat sheets and the official manual
A cheat sheet is best for looking up familiar syntax quickly; it is not a substitute for understanding why Bash interprets a command a particular way. The official GNU manual is the stronger reference for behavior and fuller explanations. GNU’s manual page offers HTML, PDF, Info, ASCII and other formats; it says it was last updated July 4, 2025. The current Bash Reference Manual is edition 5.3, updated May 18, 2025.
| Resource | Authority and depth | Format and best use |
|---|---|---|
| GNU Bash manual | Official GNU documentation; full manual, rather than a quick summary. | HTML and downloadable PDF, Info, ASCII and other formats; use to learn or verify Bash behavior. |
| Red Hat Bash shell cheat sheet | Third-party quick reference, not a replacement for the full manual. | Web page with a downloadable guide and examples; useful for a fast syntax lookup. |
| Linuxize Bash cheatsheet | Third-party quick reference covering core scripting concepts such as variables, conditionals, loops and functions. | Web page with PDF download/print options; the page reports an update date of August 17, 2026. |
For the companion download, the Red Hat sheet and Linuxize sheet are the quick-reference options listed above. Check each live page for its current download or print controls, which may change. The GNU manual is the place to go when a short syntax entry does not explain a behavior you need to understand.
Free tools Windows power users keep installed
One-click scans. No signup required.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




