374 lines
7.8 KiB
Plaintext
374 lines
7.8 KiB
Plaintext
=============================
|
|
Literal words
|
|
=============================
|
|
|
|
echo a
|
|
echo a b
|
|
|
|
---
|
|
|
|
(program
|
|
(command (command_name (word)) (word))
|
|
(command (command_name (word)) (word) (word)))
|
|
|
|
=============================
|
|
Words with special characters
|
|
=============================
|
|
|
|
echo {o[k]}
|
|
echo }}}
|
|
echo ]]] ===
|
|
|
|
---
|
|
|
|
(program
|
|
(command (command_name (word)) (concatenation (word) (word)))
|
|
(command (command_name (word)) (word))
|
|
(command (command_name (word)) (word) (word)))
|
|
|
|
=============================
|
|
The regex operator
|
|
=============================
|
|
|
|
[[ "35d8b" =~ ^[0-9a-fA-F] ]]
|
|
[[ $CMD =~ (^|;)update_terminal_cwd($|;) ]]
|
|
[[ ! " ${completions[*]} " =~ " $alias_cmd " ]]
|
|
|
|
---
|
|
|
|
(program
|
|
(bracket_command
|
|
(string)
|
|
(regex))
|
|
(bracket_command
|
|
(simple_expansion (variable_name))
|
|
(regex))
|
|
(bracket_command
|
|
(word)
|
|
(string (expansion (subscript (variable_name) (word))))
|
|
(string (simple_expansion (variable_name)))))
|
|
|
|
=============================
|
|
Simple variable expansions
|
|
=============================
|
|
|
|
echo $abc
|
|
|
|
---
|
|
|
|
(program
|
|
(command (command_name (word)) (simple_expansion (variable_name))))
|
|
|
|
=============================
|
|
Special variable expansions
|
|
=============================
|
|
|
|
echo $# $* $@
|
|
|
|
---
|
|
|
|
(program
|
|
(command
|
|
(command_name (word))
|
|
(simple_expansion (special_variable_name))
|
|
(simple_expansion (special_variable_name))
|
|
(simple_expansion (special_variable_name))))
|
|
|
|
=============================
|
|
Variable expansions
|
|
=============================
|
|
|
|
echo ${abc}
|
|
echo ${abc:-def}
|
|
echo ${abc:- }
|
|
echo ${abc:
|
|
}
|
|
|
|
---
|
|
|
|
(program
|
|
(command (command_name (word)) (expansion (variable_name)))
|
|
(command (command_name (word)) (expansion (variable_name) (word)))
|
|
(command (command_name (word)) (expansion (variable_name)))
|
|
(command (command_name (word)) (expansion (variable_name))))
|
|
|
|
===================================
|
|
Variable expansions with operators
|
|
===================================
|
|
|
|
A="${B[0]# }"
|
|
C="${D/#* -E /}"
|
|
F="${G%% *}"
|
|
|
|
---
|
|
|
|
(program
|
|
(variable_assignment
|
|
(variable_name)
|
|
(string (expansion (subscript (variable_name) (word)))))
|
|
(variable_assignment
|
|
(variable_name)
|
|
(string (expansion (variable_name) (word) (word))))
|
|
(variable_assignment
|
|
(variable_name)
|
|
(string (expansion (variable_name) (word) (word)))))
|
|
|
|
===================================
|
|
Variable expansions in strings
|
|
===================================
|
|
|
|
A="${A:-$B/c}"
|
|
A="${b/$c/$d}"
|
|
|
|
---
|
|
|
|
(program
|
|
(variable_assignment
|
|
(variable_name)
|
|
(string
|
|
(expansion
|
|
(variable_name)
|
|
(concatenation (simple_expansion (variable_name)) (word)))))
|
|
(variable_assignment
|
|
(variable_name)
|
|
(string
|
|
(expansion
|
|
(variable_name)
|
|
(concatenation
|
|
(simple_expansion (variable_name))
|
|
(word)
|
|
(simple_expansion (variable_name)))))))
|
|
|
|
===================================
|
|
Other variable expansion operators
|
|
===================================
|
|
|
|
cat ${BAR} ${ABC=def} ${GHI:?jkl}
|
|
|
|
---
|
|
|
|
(program
|
|
(command
|
|
(command_name (word))
|
|
(expansion (variable_name))
|
|
(expansion (variable_name) (word))
|
|
(expansion (variable_name) (word))))
|
|
|
|
=============================
|
|
Command substitutions
|
|
=============================
|
|
|
|
echo `echo hi`
|
|
echo $(echo $(echo hi))
|
|
|
|
---
|
|
|
|
(program
|
|
(command
|
|
(command_name (word))
|
|
(command_substitution (command (command_name (word)) (word))))
|
|
(command
|
|
(command_name (word))
|
|
(command_substitution (command
|
|
(command_name (word))
|
|
(command_substitution (command
|
|
(command_name (word))
|
|
(word)))))))
|
|
|
|
=============================
|
|
Process substitutions
|
|
=============================
|
|
|
|
wc -c <(echo abc && echo def)
|
|
echo abc > >(wc -c)
|
|
|
|
---
|
|
|
|
(program
|
|
(command
|
|
(command_name (word))
|
|
(word)
|
|
(process_substitution (list
|
|
(command (command_name (word)) (word))
|
|
(command (command_name (word)) (word)))))
|
|
(command
|
|
(command_name (word))
|
|
(word)
|
|
(file_redirect (process_substitution
|
|
(command (command_name (word)) (word))))))
|
|
|
|
=============================
|
|
Single quoted strings
|
|
=============================
|
|
|
|
echo 'a b' 'c d'
|
|
|
|
---
|
|
|
|
(program
|
|
(command (command_name (word)) (raw_string) (raw_string)))
|
|
|
|
=============================
|
|
Double quoted strings
|
|
=============================
|
|
|
|
echo "a" "b"
|
|
echo "a ${b} c" "d $e"
|
|
|
|
---
|
|
|
|
(program
|
|
(command (command_name (word))
|
|
(string)
|
|
(string))
|
|
(command (command_name (word))
|
|
(string (expansion (variable_name)))
|
|
(string (simple_expansion (variable_name)))))
|
|
|
|
=========================================
|
|
Strings containing command substitutions
|
|
=========================================
|
|
|
|
find "`dirname $file`" -name "$base"'*'
|
|
|
|
---
|
|
|
|
(program
|
|
(command
|
|
(command_name (word))
|
|
(string (command_substitution (command (command_name (word)) (simple_expansion (variable_name)))))
|
|
(word)
|
|
(concatenation
|
|
(string (simple_expansion (variable_name)))
|
|
(raw_string))))
|
|
|
|
=========================================
|
|
Strings containing escape sequence
|
|
=========================================
|
|
|
|
echo "\"The great escape\`\${var}"
|
|
|
|
---
|
|
|
|
(program (command (command_name (word)) (string)))
|
|
|
|
======================================
|
|
Strings containing special characters
|
|
======================================
|
|
|
|
echo "s/$/'/"
|
|
echo "#"
|
|
|
|
---
|
|
|
|
(program
|
|
(command (command_name (word)) (string))
|
|
(command (command_name (word)) (string)))
|
|
|
|
=========================================
|
|
Variable declaration: declare & typeset
|
|
=========================================
|
|
|
|
declare var1
|
|
typeset -i -r var2=42 var3=10
|
|
|
|
---
|
|
|
|
(program
|
|
(declaration_command (variable_name))
|
|
(declaration_command (word) (word)
|
|
(variable_assignment (variable_name) (word))
|
|
(variable_assignment (variable_name) (word))))
|
|
|
|
=========================================
|
|
Variable declaration: readonly
|
|
=========================================
|
|
|
|
readonly var1
|
|
readonly var2=42
|
|
|
|
---
|
|
|
|
(program
|
|
(declaration_command (variable_name))
|
|
(declaration_command (variable_assignment (variable_name) (word))))
|
|
|
|
=========================================
|
|
Variable declaration: local
|
|
=========================================
|
|
|
|
local a=42 b
|
|
local -r c
|
|
|
|
---
|
|
|
|
(program
|
|
(declaration_command
|
|
(variable_assignment (variable_name) (word))
|
|
(variable_name))
|
|
(declaration_command
|
|
(word)
|
|
(variable_name)))
|
|
|
|
=========================================
|
|
Variable declaration: export
|
|
=========================================
|
|
|
|
export PATH
|
|
export FOOBAR PATH="$PATH:/usr/foobar/bin"
|
|
|
|
---
|
|
|
|
(program
|
|
(declaration_command (variable_name))
|
|
(declaration_command
|
|
(variable_name)
|
|
(variable_assignment (variable_name) (string (simple_expansion (variable_name))))))
|
|
|
|
===========================================
|
|
Expressions passed to declaration commands
|
|
===========================================
|
|
|
|
export "$(echo ${key} | tr [:lower:] [:upper:])=${p_key#*=}"
|
|
|
|
---
|
|
|
|
(program
|
|
(declaration_command
|
|
(string
|
|
(command_substitution
|
|
(pipeline
|
|
(command (command_name (word)) (expansion (variable_name)))
|
|
(command (command_name (word)) (concatenation (word)) (concatenation (word)))))
|
|
(expansion (variable_name) (word)))))
|
|
|
|
=========================================
|
|
Arrays and array expansions
|
|
=========================================
|
|
|
|
a=()
|
|
b=(1 2 3)
|
|
|
|
echo ${a[@]}
|
|
echo ${#b[@]}
|
|
|
|
a[$i]=50
|
|
a+=(foo "bar" $(baz))
|
|
|
|
---
|
|
|
|
(program
|
|
(variable_assignment (variable_name) (array))
|
|
(variable_assignment (variable_name) (array (word) (word) (word)))
|
|
(command (command_name (word)) (expansion (subscript (variable_name) (word))))
|
|
(command (command_name (word)) (expansion (subscript (variable_name) (word))))
|
|
(variable_assignment
|
|
(subscript (variable_name) (simple_expansion (variable_name)))
|
|
(word))
|
|
(variable_assignment
|
|
(variable_name)
|
|
(array
|
|
(word)
|
|
(string)
|
|
(command_substitution (command (command_name (word)))))))
|