Special Characters

Published

2025-06-25

Caution

This section is under development. Thank you for your patience.

Some characters such as curly braces, single quotes, ampersand, and parentheses have special meanings in the shell. They can be used with arguments to add search, match, and print functionality.

Braces: {}

Brace Expansion ({}) allows the creation of multiple text strings from a pattern containing braces.

Example

List .tsv and .csv files named wu_tang:

ls data/wu_tang.{tsv,psv}
# data/wu_tang.psv
# data/wu_tang.tsv

Expands into:

cat data/wu_tang.tsv 
# Member    Name
# RZA   Robert Diggs
# GZA   Gary Grice
# Method Man    Clifford Smith
# Raekwon the Chef  Corey Woods
# Ghostface Killah  Dennis Coles
# Inspectah Deck    Jason Hunter
# U-God Lamont Hawkins
# Masta Killa   Jamel Irief
# Cappadonna    Darryl Hill
# Ol Dirty Bastard  Russell Tyrone Jones
cat data/wu_tang.psv
# |Member           |Name                 |
# |RZA              |Robert Diggs         |
# |GZA              |Gary Grice           |
# |Method Man       |Clifford Smith       |
# |Raekwon the Chef |Corey Woods          |
# |Ghostface Killah |Dennis Coles         |
# |Inspectah Deck   |Jason Hunter         |
# |U-God            |Lamont Hawkins       |
# |Masta Killa      |Jamel Irief          |
# |Cappadonna       |Darryl Hill          |
# |Ol Dirty Bastard |Russell Tyrone Jones |

Backslash: \

\ escapes the following character, nullifying its special meaning

Example

Print text with spaces and the ampersand:

echo "File name with spaces \& special characters"
# File name with spaces & special characters

Single quotes: ''

Single quotes (' ') treat every character literally, ignoring the special meaning of all characters.

Example

echo '$HOME' prints $HOME, not the path to the home directory:

echo '$HOME'
# $HOME

Double quotes: ""

Double quotes (" ") allow for the inclusion of special characters in an argument, except for the dollar sign ($), backticks (` `), and backslash (\).

Example

echo "$HOME" prints the path to the home directory:

echo "$HOME"
#> /Users/username

Tilde: ~

~ represents the home directory of the current user.

Example

Print a folder tree of the items in the user’s Library/Python directory:

tree ~/Library/Python -L 1
#> /Users/username/Library/Python
#> └── 3.9
#>
#> 2 directories, 0 files

Dollar Sign: $

$ indicates a variable.

Example

echo $PATH prints the value of the PATH environment variable:

echo $PATH

Ampersand: &

& runs a command in the background.

Example

firefox & opens Firefox in the background, allowing the terminal to be used for other commands.

firefox &

Semicolon: ;

; separates multiple commands to be run in sequence.

Example

Change the directory to data/ and then print a folder tree of it’s contents with the full path (-f), size (-s) in SI units (powers of 1000) in a more human readable way (-h).

cd data/; tree -f -s --si -h
#> [ 320]  .
#> β”œβ”€β”€ [6.1k]  ./music_vids.tsv
#> β”œβ”€β”€ [ 12k]  ./pwrds.tsv
#> β”œβ”€β”€ [ 320]  ./raw
#> β”‚   β”œβ”€β”€ [ 13k]  ./raw/ajperlis_epigrams.txt
#> β”‚   β”œβ”€β”€ [6.5k]  ./raw/music_vids.csv
#> β”‚   β”œβ”€β”€ [ 12k]  ./raw/pwrds.csv
#> β”‚   β”œβ”€β”€ [1.3k]  ./raw/roxanne.txt
#> β”‚   β”œβ”€β”€ [4.5k]  ./raw/trees.csv
#> β”‚   β”œβ”€β”€ [4.8k]  ./raw/vg_hof.csv
#> β”‚   β”œβ”€β”€ [ 381]  ./raw/who_tb_data.csv
#> β”‚   └── [ 263]  ./raw/wu_tang.csv
#> β”œβ”€β”€ [9.0k]  ./README.md
#> β”œβ”€β”€ [4.4k]  ./trees.tsv
#> β”œβ”€β”€ [4.8k]  ./vg_hof.tsv
#> β”œβ”€β”€ [ 462]  ./wu_tang.psv
#> └── [ 263]  ./wu_tang.tsv
#> 
#> 2 directories, 15 files

Greater Than: >

Redirection operators: > directs output to a file or a device.

Example

echo "This is my 2nd file" > myfile2.txt writes "This is my 2nd file" into myfile2.txt:

echo "This is my 2nd file" > myfile2.txt

Less Than: <

Redirection operators: < takes input from a file or a device.

Example

Then wc < myfile2.txt counts the words in myfile2.txt:

wc < myfile2.txt
#        1       5      20

Parentheses: ()

Parentheses can be used to group commands or for command substitution with $( ).

Example

Runs ls in /data without changing the current directory:

(cd data; ls)
# confirm working directory hasn't changed 
pwd
# music_vids.tsv
# pwrds.tsv
# raw
# README.md
# trees.tsv
# vg_hof.tsv
# wu_tang.psv
# wu_tang.tsv
# /Users/mjfrigaard/projects/books/fm-linux

$(command) uses the output of command.

See a typo, error, or something missing?

Please open an issue on GitHub.