DEV Community

Cover image for Bash cheat sheet
d-yogesh
d-yogesh

Posted on

Bash cheat sheet

Notes I wrote reading the bash wiki


Bash Behavior & Syntax Cheat Sheet

Argument Parsing & Quoting

1. Quoted arguments are passed as a single argument

echo "hello world"   # one argument: "hello world"
echo hello world     # two arguments: "hello" and "world"
Enter fullscreen mode Exit fullscreen mode

2. Single quotes prevent all expansions

echo '$HOME'   # prints: $HOME
echo "$HOME"   # expands to: /home/user
Enter fullscreen mode Exit fullscreen mode

3. Always quote variable expansions ("$var")

Prevents word splitting + glob expansion.

name="a b * c"
echo $name     # BAD: splits; globs expand
echo "$name"   # GOOD: literal content
Enter fullscreen mode Exit fullscreen mode

Tests: [ vs [[

4. [ is a builtin (POSIX test), [[ is a Bash keyword

[ "$x" = "abc" ]      # POSIX test
[[ $x == abc ]]       # Bash test, supports patterns, safer
Enter fullscreen mode Exit fullscreen mode

5. [[ uses pattern matching unless the right side is quoted

x="hello"

[[ $x = h* ]]     # true (pattern match)
[[ $x = "h*" ]]   # false (literal match)
Enter fullscreen mode Exit fullscreen mode

Globbing

6. Bash filename expansion uses globs, not regular expressions

❌ Not valid:

ls *.txt$
Enter fullscreen mode Exit fullscreen mode

✔ Correct:

echo *.txt
Enter fullscreen mode Exit fullscreen mode

7. Extended globs (+( ), !( ), etc.) require enabling

shopt -s extglob
echo +([0-9]).log
Enter fullscreen mode Exit fullscreen mode

8. Globs can expand even when partially quoted

echo "*.txt"    # no expansion → prints "*.txt"
echo *".txt"    # expands → file1.txt file2.txt
Enter fullscreen mode Exit fullscreen mode

Grouping & Control Flow

9. Group commands with {} when using && / ||

{ echo one; echo two; } && echo "group succeeded"
Enter fullscreen mode Exit fullscreen mode

10. Grouping allows redirecting multiple commands at once

Redirect output:

{
    echo line1
    echo line2
} > out.txt
Enter fullscreen mode Exit fullscreen mode

Redirect input:

{
    read a
    read b
    echo "$a and $b"
} < file.txt
Enter fullscreen mode Exit fullscreen mode

Pipelines & Subshells

11. Pipe (|) connects stdout → stdin

echo "hello" | tr a-z A-Z
Enter fullscreen mode Exit fullscreen mode

12. Each side of a pipe runs in a subshell

x=1
echo hi | { read l; x=2; }
echo "$x"     # still 1
Enter fullscreen mode Exit fullscreen mode

13. Parentheses ( ) also create a subshell

x=1
( x=2 )
echo "$x"   # prints 1
Enter fullscreen mode Exit fullscreen mode

14. Braces { } do NOT create a subshell

x=1
{ x=2; }
echo "$x"   # prints 2
Enter fullscreen mode Exit fullscreen mode

Sourcing

15. source or . runs a script in the current shell

script.sh:

x=42
Enter fullscreen mode Exit fullscreen mode

Using source:

x=1
source script.sh
echo "$x"   # prints 42
Enter fullscreen mode Exit fullscreen mode

Executing script directly:

x=1
./script.sh
echo "$x"   # prints 1 (runs in subshell)
Enter fullscreen mode Exit fullscreen mode

Dot form:

. script.sh
Enter fullscreen mode Exit fullscreen mode

Top comments (0)