2017-07-16 05:13:55 +00:00
|
|
|
=============================
|
|
|
|
Literal words
|
|
|
|
=============================
|
|
|
|
|
|
|
|
echo a
|
|
|
|
echo a b
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
2017-07-17 17:19:35 +00:00
|
|
|
(command (command_name (word)) (word))
|
|
|
|
(command (command_name (word)) (word) (word)))
|
2017-07-16 05:13:55 +00:00
|
|
|
|
|
|
|
=============================
|
|
|
|
Simple variable expansions
|
|
|
|
=============================
|
|
|
|
|
|
|
|
echo $abc
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
2017-07-17 17:19:35 +00:00
|
|
|
(command (command_name (word)) (simple_expansion (variable_name))))
|
2017-07-16 05:13:55 +00:00
|
|
|
|
|
|
|
=============================
|
|
|
|
Variable expansions
|
|
|
|
=============================
|
|
|
|
|
|
|
|
echo ${abc}
|
|
|
|
echo ${abc:-def}
|
2018-02-24 21:40:43 +00:00
|
|
|
echo ${abc:- }
|
|
|
|
echo ${abc:
|
|
|
|
}
|
2017-07-16 05:13:55 +00:00
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
2017-07-17 17:19:35 +00:00
|
|
|
(command (command_name (word)) (expansion (variable_name)))
|
2018-02-24 21:40:43 +00:00
|
|
|
(command (command_name (word)) (expansion (variable_name) (word)))
|
|
|
|
(command (command_name (word)) (expansion (variable_name)))
|
|
|
|
(command (command_name (word)) (expansion (variable_name))))
|
2017-07-16 05:13:55 +00:00
|
|
|
|
|
|
|
===================================
|
|
|
|
Other variable expansion operators
|
|
|
|
===================================
|
|
|
|
|
|
|
|
cat ${BAR} ${ABC=def} ${GHI:?jkl}
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
|
|
|
(command
|
2017-07-17 17:19:35 +00:00
|
|
|
(command_name (word))
|
2017-07-16 05:13:55 +00:00
|
|
|
(expansion (variable_name))
|
|
|
|
(expansion (variable_name) (word))
|
|
|
|
(expansion (variable_name) (word))))
|
|
|
|
|
|
|
|
=============================
|
|
|
|
Command substitutions
|
|
|
|
=============================
|
|
|
|
|
|
|
|
echo `echo hi`
|
|
|
|
echo $(echo $(echo hi))
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
|
|
|
(command
|
2017-07-17 17:19:35 +00:00
|
|
|
(command_name (word))
|
|
|
|
(command_substitution (command (command_name (word)) (word))))
|
2017-07-16 05:13:55 +00:00
|
|
|
(command
|
2017-07-17 17:19:35 +00:00
|
|
|
(command_name (word))
|
2017-07-16 05:13:55 +00:00
|
|
|
(command_substitution (command
|
2017-07-17 17:19:35 +00:00
|
|
|
(command_name (word))
|
2017-07-16 05:13:55 +00:00
|
|
|
(command_substitution (command
|
2017-07-17 17:19:35 +00:00
|
|
|
(command_name (word))
|
2017-07-16 05:13:55 +00:00
|
|
|
(word)))))))
|
|
|
|
|
2017-07-16 05:22:38 +00:00
|
|
|
=============================
|
|
|
|
Process substitutions
|
|
|
|
=============================
|
|
|
|
|
|
|
|
wc -c <(echo abc && echo def)
|
|
|
|
echo abc > >(wc -c)
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
|
|
|
(command
|
2017-07-17 17:19:35 +00:00
|
|
|
(command_name (word))
|
2017-07-16 05:22:38 +00:00
|
|
|
(word)
|
|
|
|
(process_substitution (list
|
2017-07-17 17:19:35 +00:00
|
|
|
(command (command_name (word)) (word))
|
|
|
|
(command (command_name (word)) (word)))))
|
2017-07-16 05:22:38 +00:00
|
|
|
(command
|
2017-07-17 17:19:35 +00:00
|
|
|
(command_name (word))
|
2017-07-16 05:22:38 +00:00
|
|
|
(word)
|
|
|
|
(file_redirect (process_substitution
|
2017-07-17 17:19:35 +00:00
|
|
|
(command (command_name (word)) (word))))))
|
2017-07-16 05:22:38 +00:00
|
|
|
|
2017-07-16 05:13:55 +00:00
|
|
|
=============================
|
|
|
|
Single quoted strings
|
|
|
|
=============================
|
|
|
|
|
|
|
|
echo 'a b' 'c d'
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
2017-07-17 17:19:35 +00:00
|
|
|
(command (command_name (word)) (raw_string) (raw_string)))
|
2017-07-16 05:13:55 +00:00
|
|
|
|
|
|
|
=============================
|
|
|
|
Double quoted strings
|
|
|
|
=============================
|
|
|
|
|
|
|
|
echo "a" "b"
|
|
|
|
echo "a ${b} c" "d $e"
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
2017-07-17 17:19:35 +00:00
|
|
|
(command (command_name (word))
|
2017-07-16 05:13:55 +00:00
|
|
|
(string)
|
|
|
|
(string))
|
2017-07-17 17:19:35 +00:00
|
|
|
(command (command_name (word))
|
2017-07-16 05:13:55 +00:00
|
|
|
(string (expansion (variable_name)))
|
|
|
|
(string (simple_expansion (variable_name)))))
|
2017-07-16 05:41:56 +00:00
|
|
|
|
|
|
|
=========================================
|
|
|
|
Strings containing command substitutions
|
|
|
|
=========================================
|
|
|
|
|
|
|
|
find "`dirname $file`" -name "$base"'*'
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
|
|
|
(command
|
2017-07-17 17:19:35 +00:00
|
|
|
(command_name (word))
|
|
|
|
(string (command_substitution (command (command_name (word)) (simple_expansion (variable_name)))))
|
2017-07-16 05:41:56 +00:00
|
|
|
(word)
|
2017-07-17 17:19:35 +00:00
|
|
|
(concatenation
|
|
|
|
(string (simple_expansion (variable_name)))
|
|
|
|
(raw_string))))
|
2017-07-16 06:12:22 +00:00
|
|
|
|
2018-02-24 23:02:24 +00:00
|
|
|
=========================================
|
2018-02-27 21:22:28 +00:00
|
|
|
Variable declaration: declare & typeset
|
2018-02-24 23:02:24 +00:00
|
|
|
=========================================
|
|
|
|
|
2018-02-27 21:22:28 +00:00
|
|
|
declare var1
|
|
|
|
typeset -i -r var2=42 var3=10
|
2018-02-24 23:02:24 +00:00
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
2018-02-27 21:22:28 +00:00
|
|
|
(declaration_command (simple_variable_name))
|
|
|
|
(declaration_command (argument) (argument)
|
|
|
|
(variable_assignment (variable_name) (word))
|
|
|
|
(variable_assignment (variable_name) (word))))
|
|
|
|
|
|
|
|
=========================================
|
|
|
|
Variable declaration: readonly
|
|
|
|
=========================================
|
|
|
|
|
|
|
|
readonly var1
|
|
|
|
readonly var2=42
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
|
|
|
(declaration_command (simple_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))
|
|
|
|
(simple_variable_name))
|
|
|
|
(declaration_command (argument) (simple_variable_name)))
|
|
|
|
|
|
|
|
=========================================
|
|
|
|
Variable declaration: export
|
|
|
|
=========================================
|
|
|
|
|
|
|
|
export PATH
|
|
|
|
export FOOBAR PATH="$PATH:/usr/foobar/bin"
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
|
|
|
(declaration_command (simple_variable_name))
|
|
|
|
(declaration_command
|
|
|
|
(simple_variable_name)
|
|
|
|
(variable_assignment (variable_name) (string (simple_expansion (variable_name))))))
|
2018-02-24 23:02:24 +00:00
|
|
|
|
2017-07-16 06:12:22 +00:00
|
|
|
=========================================
|
|
|
|
Arrays and array expansions
|
|
|
|
=========================================
|
|
|
|
|
|
|
|
a=()
|
|
|
|
b=(1 2 3)
|
|
|
|
|
|
|
|
echo ${a[@]}
|
|
|
|
echo ${#b[@]}
|
|
|
|
|
2017-07-17 17:19:35 +00:00
|
|
|
a[$i]=50
|
2017-12-26 22:55:37 +00:00
|
|
|
a+=(foo "bar" $(baz))
|
2017-07-17 17:19:35 +00:00
|
|
|
|
2017-07-16 06:12:22 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
2018-02-27 21:22:28 +00:00
|
|
|
(variable_assignment (variable_name) (array))
|
|
|
|
(variable_assignment (variable_name) (array (word) (word) (word)))
|
2017-07-17 17:19:35 +00:00
|
|
|
(command (command_name (word)) (expansion (variable_name)))
|
|
|
|
(command (command_name (word)) (expansion (variable_name)))
|
2018-02-27 21:22:28 +00:00
|
|
|
(variable_assignment
|
2017-07-17 17:19:35 +00:00
|
|
|
(subscript (variable_name) (simple_expansion (variable_name)))
|
2017-12-26 22:55:37 +00:00
|
|
|
(word))
|
2018-02-27 21:22:28 +00:00
|
|
|
(variable_assignment
|
2017-12-26 22:55:37 +00:00
|
|
|
(variable_name)
|
|
|
|
(array
|
|
|
|
(word)
|
|
|
|
(string)
|
|
|
|
(command_substitution (command (command_name (word)))))))
|