2018-05-24 18:46:57 +00:00
|
|
|
===================================
|
|
|
|
Pipelines
|
|
|
|
===================================
|
|
|
|
|
|
|
|
whoami | cat
|
|
|
|
cat foo | grep -v bar
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
|
|
|
(pipeline
|
|
|
|
(command (command_name (word)))
|
|
|
|
(command (command_name (word))))
|
|
|
|
(pipeline
|
|
|
|
(command (command_name (word)) (word))
|
|
|
|
(command (command_name (word)) (word) (word))))
|
|
|
|
|
|
|
|
===================================
|
|
|
|
Lists
|
|
|
|
===================================
|
|
|
|
|
|
|
|
a | b && c && d; d e f || e g
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
|
|
|
(list
|
|
|
|
(list
|
|
|
|
(pipeline
|
|
|
|
(command (command_name (word)))
|
|
|
|
(command (command_name (word))))
|
|
|
|
(command (command_name (word))))
|
|
|
|
(command (command_name (word))))
|
|
|
|
(list
|
|
|
|
(command (command_name (word)) (word) (word))
|
|
|
|
(command (command_name (word)) (word))))
|
|
|
|
|
2017-07-14 23:11:35 +00:00
|
|
|
====================================
|
|
|
|
While statements
|
|
|
|
====================================
|
|
|
|
|
|
|
|
while something happens; do
|
|
|
|
echo a
|
|
|
|
echo b
|
|
|
|
done
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
|
|
|
(while_statement
|
2017-07-17 17:19:35 +00:00
|
|
|
(command (command_name (word)) (word))
|
2017-07-14 23:11:35 +00:00
|
|
|
(do_group
|
2017-07-17 17:19:35 +00:00
|
|
|
(command (command_name (word)) (word))
|
|
|
|
(command (command_name (word)) (word)))))
|
2017-07-14 23:18:46 +00:00
|
|
|
|
2018-03-01 00:28:30 +00:00
|
|
|
====================================
|
|
|
|
While statements with IO redirects
|
|
|
|
====================================
|
|
|
|
|
|
|
|
while read line; do
|
|
|
|
echo $line
|
|
|
|
done < <(cat file)
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
|
|
|
(while_statement
|
|
|
|
(command (command_name (word)) (word))
|
|
|
|
(do_group
|
|
|
|
(command (command_name (word)) (simple_expansion (variable_name))))
|
|
|
|
(file_redirect (process_substitution
|
|
|
|
(command (command_name (word)) (word))))))
|
|
|
|
|
2017-07-15 00:51:06 +00:00
|
|
|
====================================
|
|
|
|
For statements
|
|
|
|
====================================
|
|
|
|
|
2017-07-17 17:19:35 +00:00
|
|
|
for a in 1 2 $(seq 5 10); do
|
2017-07-15 00:51:06 +00:00
|
|
|
echo $a
|
|
|
|
done
|
|
|
|
|
2018-03-02 18:18:12 +00:00
|
|
|
for ARG; do
|
|
|
|
echo $ARG
|
|
|
|
ARG=''
|
|
|
|
done
|
|
|
|
|
2017-07-15 00:51:06 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
|
|
|
(for_statement
|
2018-02-27 18:54:40 +00:00
|
|
|
(variable_name)
|
2017-07-17 17:19:35 +00:00
|
|
|
(word)
|
|
|
|
(word)
|
|
|
|
(command_substitution (command (command_name (word)) (word) (word)))
|
2017-07-15 00:51:06 +00:00
|
|
|
(do_group
|
2018-03-02 18:18:12 +00:00
|
|
|
(command (command_name (word)) (simple_expansion (variable_name)))))
|
|
|
|
(for_statement
|
|
|
|
(variable_name)
|
|
|
|
(do_group
|
|
|
|
(command (command_name (word)) (simple_expansion (variable_name)))
|
|
|
|
(variable_assignment (variable_name) (raw_string)))))
|
2017-07-15 00:51:06 +00:00
|
|
|
|
2018-08-06 02:25:19 +00:00
|
|
|
====================================
|
|
|
|
C-style for statements
|
|
|
|
====================================
|
|
|
|
|
|
|
|
for (( c=1; c<=5; c++ ))
|
|
|
|
do
|
|
|
|
echo $c
|
|
|
|
done
|
|
|
|
|
|
|
|
for (( c=1; c<=5; c++ )) {
|
|
|
|
echo $c
|
|
|
|
}
|
|
|
|
|
|
|
|
for (( ; ; ))
|
|
|
|
do
|
|
|
|
echo 'forever'
|
|
|
|
done
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
|
|
|
(c_style_for_statement
|
|
|
|
(word)
|
|
|
|
(binary_expression (word) (word))
|
|
|
|
(word)
|
|
|
|
(do_group
|
|
|
|
(command (command_name (word)) (simple_expansion (variable_name)))))
|
|
|
|
(c_style_for_statement
|
|
|
|
(word)
|
|
|
|
(binary_expression (word) (word))
|
|
|
|
(word)
|
|
|
|
(compound_statement
|
|
|
|
(command (command_name (word)) (simple_expansion (variable_name)))))
|
|
|
|
(c_style_for_statement
|
|
|
|
(do_group
|
|
|
|
(command (command_name (word)) (raw_string)))))
|
|
|
|
|
2017-07-14 23:18:46 +00:00
|
|
|
====================================
|
|
|
|
If statements
|
|
|
|
====================================
|
|
|
|
|
|
|
|
if cat some_file | grep -v ok; then
|
|
|
|
echo one
|
|
|
|
elif cat other_file | grep -v ok; then
|
|
|
|
echo two
|
|
|
|
else
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
|
|
|
(if_statement
|
|
|
|
(pipeline
|
2017-07-17 17:19:35 +00:00
|
|
|
(command (command_name (word)) (word))
|
|
|
|
(command (command_name (word)) (word) (word)))
|
|
|
|
(command (command_name (word)) (word))
|
2017-07-14 23:18:46 +00:00
|
|
|
(elif_clause
|
|
|
|
(pipeline
|
2017-07-17 17:19:35 +00:00
|
|
|
(command (command_name (word)) (word))
|
|
|
|
(command (command_name (word)) (word) (word)))
|
|
|
|
(command (command_name (word)) (word)))
|
2017-07-14 23:18:46 +00:00
|
|
|
(else_clause
|
2017-07-17 17:19:35 +00:00
|
|
|
(command (command_name (word))))))
|
2017-07-14 23:29:28 +00:00
|
|
|
|
2017-07-14 23:47:25 +00:00
|
|
|
====================================
|
|
|
|
If statements with conditional expressions
|
|
|
|
====================================
|
|
|
|
|
|
|
|
if [ "$(uname)" == 'Darwin' ]; then
|
|
|
|
echo one
|
|
|
|
fi
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
|
|
|
(if_statement
|
2018-05-24 18:46:57 +00:00
|
|
|
(test_command (binary_expression
|
2017-07-17 17:19:35 +00:00
|
|
|
(string (command_substitution (command (command_name (word)))))
|
2018-05-24 18:46:57 +00:00
|
|
|
(raw_string)))
|
2017-07-17 17:19:35 +00:00
|
|
|
(command (command_name (word)) (word))))
|
2017-07-14 23:47:25 +00:00
|
|
|
|
2017-07-14 23:29:28 +00:00
|
|
|
====================================
|
|
|
|
Case statements
|
|
|
|
====================================
|
|
|
|
|
|
|
|
case "opt" in
|
|
|
|
a)
|
|
|
|
echo a
|
|
|
|
;;
|
|
|
|
|
|
|
|
b)
|
2018-03-01 22:04:29 +00:00
|
|
|
echo b;;
|
2017-07-14 23:29:28 +00:00
|
|
|
esac
|
|
|
|
|
2018-03-01 00:29:42 +00:00
|
|
|
case "$Z" in
|
|
|
|
ab*|cd*) ef
|
|
|
|
esac
|
|
|
|
|
2018-03-16 22:41:09 +00:00
|
|
|
case $dest in
|
|
|
|
*.[1357])
|
|
|
|
exit $?
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2017-07-14 23:29:28 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
2017-07-16 05:13:55 +00:00
|
|
|
(case_statement (string)
|
|
|
|
(case_item (word)
|
2017-07-17 17:19:35 +00:00
|
|
|
(command (command_name (word)) (word)))
|
2017-07-16 05:13:55 +00:00
|
|
|
(case_item (word)
|
2018-03-01 00:29:42 +00:00
|
|
|
(command (command_name (word)) (word))))
|
|
|
|
(case_statement (string (simple_expansion (variable_name)))
|
|
|
|
(case_item (word) (word)
|
2018-03-16 22:41:09 +00:00
|
|
|
(command (command_name (word)))))
|
|
|
|
(case_statement (simple_expansion (variable_name))
|
|
|
|
(case_item (concatenation (word) (word))
|
|
|
|
(command (command_name (word)) (simple_expansion (special_variable_name))))))
|
2017-07-15 00:32:55 +00:00
|
|
|
|
2018-05-24 18:46:57 +00:00
|
|
|
=============================
|
|
|
|
Test commands
|
|
|
|
=============================
|
|
|
|
|
|
|
|
if [[ "$lsb_dist" != 'Ubuntu' || $(ver_to_int "$lsb_release") < $(ver_to_int '14.04') ]]; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
|
|
|
(if_statement
|
|
|
|
(test_command (binary_expression
|
|
|
|
(binary_expression
|
|
|
|
(binary_expression
|
|
|
|
(string (simple_expansion (variable_name)))
|
|
|
|
(raw_string))
|
|
|
|
(command_substitution (command
|
|
|
|
(command_name (word))
|
|
|
|
(string (simple_expansion (variable_name))))))
|
|
|
|
(command_substitution (command (command_name (word)) (raw_string)))))
|
|
|
|
(command (command_name (word)) (word))))
|
|
|
|
|
|
|
|
=============================
|
|
|
|
Test commands with regexes
|
|
|
|
=============================
|
|
|
|
|
|
|
|
[[ "35d8b" =~ ^[0-9a-fA-F] ]]
|
|
|
|
[[ $CMD =~ (^|;)update_terminal_cwd($|;) ]]
|
|
|
|
[[ ! " ${completions[*]} " =~ " $alias_cmd " ]]
|
|
|
|
! [[ "$a" =~ ^a|b\ *c|d$ ]]
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
|
|
|
(test_command
|
|
|
|
(binary_expression
|
|
|
|
(string)
|
|
|
|
(regex)))
|
|
|
|
(test_command
|
|
|
|
(binary_expression
|
|
|
|
(simple_expansion (variable_name))
|
|
|
|
(regex)))
|
|
|
|
(test_command
|
|
|
|
(unary_expression
|
|
|
|
(binary_expression
|
|
|
|
(string (expansion (subscript (variable_name) (word))))
|
|
|
|
(string (simple_expansion (variable_name))))))
|
|
|
|
(negated_command
|
|
|
|
(test_command
|
|
|
|
(binary_expression
|
|
|
|
(string (simple_expansion (variable_name)))
|
|
|
|
(regex)))))
|
|
|
|
|
2017-07-15 00:32:55 +00:00
|
|
|
===============================
|
|
|
|
Subshells
|
|
|
|
===============================
|
|
|
|
|
|
|
|
(
|
|
|
|
./start-server --port=80
|
|
|
|
) &
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
2017-07-17 17:19:35 +00:00
|
|
|
(subshell (command (command_name (word)) (word))))
|
2017-07-15 00:35:51 +00:00
|
|
|
|
|
|
|
===============================
|
|
|
|
Function definitions
|
|
|
|
===============================
|
|
|
|
|
|
|
|
do_something() {
|
|
|
|
echo ok
|
|
|
|
}
|
|
|
|
|
|
|
|
function do_something_else() {
|
|
|
|
echo ok
|
|
|
|
}
|
|
|
|
|
2017-12-26 22:27:05 +00:00
|
|
|
function do_yet_another_thing {
|
|
|
|
echo ok
|
|
|
|
} 2>&1
|
|
|
|
|
2017-07-15 00:35:51 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
2017-12-26 22:27:05 +00:00
|
|
|
(function_definition
|
2018-02-28 19:13:49 +00:00
|
|
|
(word)
|
2017-12-26 22:27:05 +00:00
|
|
|
(compound_statement (command (command_name (word)) (word))))
|
|
|
|
(function_definition
|
2018-02-28 19:13:49 +00:00
|
|
|
(word)
|
2017-12-26 22:27:05 +00:00
|
|
|
(compound_statement (command (command_name (word)) (word))))
|
|
|
|
(function_definition
|
2018-02-28 19:13:49 +00:00
|
|
|
(word)
|
2017-12-26 22:27:05 +00:00
|
|
|
(compound_statement (command (command_name (word)) (word)))
|
|
|
|
(file_redirect (file_descriptor) (word))))
|
2018-03-01 18:12:10 +00:00
|
|
|
|
|
|
|
=========================================
|
|
|
|
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))))))
|
|
|
|
|
2018-08-06 17:39:05 +00:00
|
|
|
===========================================================
|
|
|
|
Variable declaration: command substitution with semi-colon
|
|
|
|
===========================================================
|
|
|
|
|
|
|
|
_path=$(
|
|
|
|
while statement; do
|
|
|
|
cd ".."
|
|
|
|
done;
|
|
|
|
echo $PWD
|
|
|
|
)
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
|
|
|
(variable_assignment (variable_name)
|
|
|
|
(command_substitution
|
|
|
|
(while_statement
|
|
|
|
(command (command_name (word)))
|
|
|
|
(do_group (command (command_name (word)) (string))))
|
|
|
|
(command (command_name (word)) (simple_expansion (variable_name))))))
|
|
|
|
|
2018-03-01 18:12:10 +00:00
|
|
|
===========================================
|
|
|
|
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)))))
|
|
|
|
|
|
|
|
=========================================
|
|
|
|
Unset commands
|
|
|
|
=========================================
|
|
|
|
|
|
|
|
unset A
|
|
|
|
unset "$variable_name"
|
|
|
|
unsetenv -f ONE TWO
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
|
|
|
(unset_command (variable_name))
|
|
|
|
(unset_command (string (simple_expansion (variable_name))))
|
|
|
|
(unset_command (word) (variable_name) (variable_name)))
|