- single quotes: no expansion at all
echo '$HOME *.txt'
- double quotes: allow variable + command substitution only
echo "home is $HOME"
echo "pwd is $(pwd)"
-
no quotes: full expansion (globs, variables, braces, etc.)
-
variables:
set foo 1 2 3
echo $foo # → 1 2 3 (multiple args)
echo "$foo" # → "1 2 3" (single arg)
- general:
\escapes next char
echo \$HOME
echo \*
echo \(
- common sequences:
\n newline
\t tab
\r carriage return
\xHH hex byte
\uXXXX unicode
\UXXXXXXXX unicode (32-bit)
- literal space:
echo hello\ world
- continuation (special case):
echo foo \
bar
order:
- command substitution
- variable expansion
- brace expansion
- wildcard expansion
echo $HOME
echo file.{c,h}
ls *.txt
ls **.log
- unmatched glob → command fails (not passed literally)
set a x y
echo pre$a # → prex prey
echo {$a}post # → xpost ypost
- syntax:
echo (pwd)
echo $(pwd)
-
behavior:
- splits on newline into multiple args
echo (printf "a\nb\n") # → a b
- prevent splitting:
echo "$(pwd)"
- common pattern:
set files (ls *.txt)
- process substitution (fish-specific):
diff (cmd1 | psub) (cmd2 | psub)
- backslash newline:
echo foo \
bar
- no whitespace inserted
- stdin: 0
- stdout: 1
- stderr: 2
echo hi > out.txt # overwrite
echo hi >> out.txt # append
cmd 2> err.txt
cmd 2>> err.txt
cmd < input.txt
cmd <? input.txt # fallback to /dev/null
cmd &> all.txt # overwrite both
cmd &>> all.txt # append both
equivalent:
cmd > all.txt 2>&1
cmd > &2 # stdout → stderr
cmd 2> &1 # stderr → stdout
cmd 2>&- # close stderr
cmd >? file.txt # fail if exists
cmd 2>? err.txt
cmd1 | cmd2
cmd 2>| less # pipe stderr
cmd &| less # pipe stdout+stderr
- pipe is created first
- then redirections left → right
example:
cmd 2>&1 | less
# safe variable usage
set f "my file.txt"
rm "$f"
# list files or nothing (no glob failure)
set files *.txt
count $files >/dev/null; and ls $files
# redirect everything
make &> build.log
# silence stdout, keep stderr
cmd > /dev/null
# silence both
cmd > /dev/null 2>&1
# stderr only through pager
make 2>| less
# inline variable override
PATH=/tmp/bin $PATH/mycmd