ls data/wu_tang.{tsv,psv}
# data/wu_tang.psv
# data/wu_tang.tsv
Special Characters
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
:
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
.