Quotes
Reference http://wiki.bash-hackers.org/syntax/quoting
- Per Character:
\$HOME
/\<newline>
(line continuation) - Double Quote:
"..."
- Escape:
- spaces
- single quotes
- pattern matching characters and path name expansion (
*
/?
/{a,b,c}
) - process substitution (
<(ls -l)
)
- Everything else remains (e.g.
"$HOME"
will be expanded) "\$"
becomes$
but"\x"
becomes\x
- Escape:
- Single Quote:
'...'
- Escape everything except
'
- Even
'\$'
becomes\$
- Even
- To use
...'...
, must enter...'\''...
or...'"'"'...
- Escape everything except
- Localized String:
$"..."
- Ignored when the locale is POSIX or C
- ANSI C String:
$'...'
- Use specific escape sequences similar to C:
\'
,\"
,\\
\a
,\b
,\e
(ESC),\f
,\n
,\r
,\t
,\v
\c♥
(CTRL + ♥ where ♥ is some character)\nnn
(octal),\xHH
(hex),\uXXXX
(unicode)
- Use specific escape sequences similar to C:
Brackets
[
=test
; the closing]
is optional[[
= enhanced[
(e.g., can use||
and&&
instead of-o
and-a
)[...]
= array indexarray[0]=value
element=${array[0]}
{...}
- Brace expansion:
{a,b,c}
/{a..z}
- Code block:
{ date; make 2>&1; date; } | tee logfile
- The last semicolon in
{...}
is mandatory {...}
must be surrounded by spaces
- The last semicolon in
- Brace expansion:
${...}
= parameter expansion${param:-word}
= ifparam
is not set, returnword
${param:=word}
= ifparam
is not set, set it toword
and returnword
${paramr:offset}
/${param:offset:length}
(negative indices work too)${#param}
= length${param#prefix}
/${param##prefix}
= remove shortest / longest prefix (mnemonic:#
comes before$
on the keyboard)${param%suffix}
/${param%%suffix}
= remove shortest / longest suffix (mnemonic:%
comes after$
on the keyboard)${param/pattern/sub}
(...)
- Array:
array=(1 2 3)
- Subshell:
<(ls -l)
/(cd .. && pwd)
will not change directory in current shell
- Array:
$(...)
= command substitution (= backticks)result=$(COMMAND)
((...))
= arithmetic((a++))
((meaning = 42))
for ((i=0; i<10; i++))
-
echo $((a + b + (14 * c)))