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
|
|
|
|
2018-02-27 18:54:40 +00:00
|
|
|
=============================
|
|
|
|
Words with special characters
|
|
|
|
=============================
|
|
|
|
|
2018-02-28 19:13:49 +00:00
|
|
|
echo {o[k]}
|
2018-02-27 18:54:40 +00:00
|
|
|
echo }}}
|
|
|
|
echo ]]] ===
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
2018-02-28 19:13:49 +00:00
|
|
|
(command (command_name (word)) (concatenation (word) (word)))
|
2018-11-04 21:14:13 +00:00
|
|
|
(command (command_name (word)) (concatenation))
|
|
|
|
(command (command_name (word)) (concatenation) (word)))
|
2018-03-01 17:53:10 +00:00
|
|
|
|
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
|
|
|
|
2018-03-01 01:32:48 +00:00
|
|
|
=============================
|
|
|
|
Special variable expansions
|
|
|
|
=============================
|
|
|
|
|
2019-04-18 22:46:58 +00:00
|
|
|
echo $# $* $@ $!
|
2018-03-01 01:32:48 +00:00
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
|
|
|
(command
|
|
|
|
(command_name (word))
|
|
|
|
(simple_expansion (special_variable_name))
|
|
|
|
(simple_expansion (special_variable_name))
|
2019-04-18 22:46:58 +00:00
|
|
|
(simple_expansion (special_variable_name))
|
2018-03-01 01:32:48 +00:00
|
|
|
(simple_expansion (special_variable_name))))
|
|
|
|
|
2017-07-16 05:13:55 +00:00
|
|
|
=============================
|
|
|
|
Variable expansions
|
|
|
|
=============================
|
|
|
|
|
2018-08-06 18:09:45 +00:00
|
|
|
echo ${var1#*#}
|
2018-08-06 17:42:48 +00:00
|
|
|
echo ${!abc}
|
2017-07-16 05:13:55 +00:00
|
|
|
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
|
2018-08-06 18:09:45 +00:00
|
|
|
(command (command_name (word)) (expansion (variable_name) (word)))
|
2018-08-06 17:42:48 +00:00
|
|
|
(command (command_name (word)) (expansion (variable_name)))
|
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
|
|
|
|
2018-03-01 00:28:55 +00:00
|
|
|
===================================
|
|
|
|
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)
|
2018-10-01 20:29:04 +00:00
|
|
|
(string (expansion (variable_name) (regex))))
|
2018-03-01 00:28:55 +00:00
|
|
|
(variable_assignment
|
|
|
|
(variable_name)
|
|
|
|
(string (expansion (variable_name) (word) (word)))))
|
|
|
|
|
2018-02-28 19:13:49 +00:00
|
|
|
===================================
|
|
|
|
Variable expansions in strings
|
|
|
|
===================================
|
|
|
|
|
|
|
|
A="${A:-$B/c}"
|
2018-03-01 18:41:16 +00:00
|
|
|
A="${b=$c/$d}"
|
2018-02-28 19:13:49 +00:00
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(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)))))))
|
|
|
|
|
2018-03-01 18:41:16 +00:00
|
|
|
===================================
|
|
|
|
Variable expansions with regexes
|
|
|
|
===================================
|
|
|
|
|
|
|
|
A=${B//:;;/$'\n'}
|
|
|
|
|
|
|
|
# escaped space
|
|
|
|
C=${D/;\ *;|}
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
|
|
|
(variable_assignment (variable_name) (expansion (variable_name) (regex)))
|
|
|
|
(comment)
|
|
|
|
(variable_assignment (variable_name) (expansion (variable_name) (regex))))
|
|
|
|
|
2017-07-16 05:13:55 +00:00
|
|
|
===================================
|
|
|
|
Other variable expansion operators
|
|
|
|
===================================
|
|
|
|
|
|
|
|
cat ${BAR} ${ABC=def} ${GHI:?jkl}
|
2018-11-04 21:14:13 +00:00
|
|
|
[ "$a" != "${a#[Bc]}" ]
|
2017-07-16 05:13:55 +00:00
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(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))
|
2018-11-04 21:14:13 +00:00
|
|
|
(expansion (variable_name) (word)))
|
|
|
|
(test_command
|
|
|
|
(binary_expression
|
|
|
|
(string (simple_expansion (variable_name)))
|
|
|
|
(string (expansion (variable_name) (concatenation (word)))))))
|
2017-07-16 05:13:55 +00:00
|
|
|
|
2018-11-04 23:33:45 +00:00
|
|
|
=============================
|
|
|
|
Words ending with '$'
|
|
|
|
=============================
|
|
|
|
|
|
|
|
grep ^${var}$
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program (command
|
|
|
|
(command_name (word))
|
|
|
|
(concatenation (word) (expansion (variable_name)))))
|
|
|
|
|
2017-07-16 05:13:55 +00:00
|
|
|
=============================
|
|
|
|
Command substitutions
|
|
|
|
=============================
|
|
|
|
|
|
|
|
echo `echo hi`
|
2018-08-06 17:39:05 +00:00
|
|
|
echo `echo hi; echo there`
|
2017-07-16 05:13:55 +00:00
|
|
|
echo $(echo $(echo hi))
|
2018-10-18 22:31:24 +00:00
|
|
|
echo $(< some-file)
|
2017-07-16 05:13:55 +00:00
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
|
|
|
(command
|
2017-07-17 17:19:35 +00:00
|
|
|
(command_name (word))
|
|
|
|
(command_substitution (command (command_name (word)) (word))))
|
2018-08-06 17:39:05 +00:00
|
|
|
(command
|
|
|
|
(command_name (word))
|
|
|
|
(command_substitution (command (command_name (word)) (word)) (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))
|
2018-10-18 22:31:24 +00:00
|
|
|
(word))))))
|
|
|
|
(command
|
|
|
|
(command_name (word))
|
|
|
|
(command_substitution (file_redirect (word)))))
|
2017-07-16 05:13:55 +00:00
|
|
|
|
2017-07-16 05:22:38 +00:00
|
|
|
=============================
|
|
|
|
Process substitutions
|
|
|
|
=============================
|
|
|
|
|
|
|
|
wc -c <(echo abc && echo def)
|
2018-08-06 17:39:05 +00:00
|
|
|
wc -c <(echo abc; echo def)
|
2017-07-16 05:22:38 +00:00
|
|
|
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)))))
|
2018-08-06 17:39:05 +00:00
|
|
|
(command
|
|
|
|
(command_name (word))
|
|
|
|
(word)
|
|
|
|
(process_substitution
|
|
|
|
(command (command_name (word)) (word))
|
|
|
|
(command (command_name (word)) (word))))
|
2018-10-18 18:05:58 +00:00
|
|
|
(redirected_statement
|
|
|
|
(command
|
|
|
|
(command_name (word))
|
|
|
|
(word))
|
2017-07-16 05:22:38 +00:00
|
|
|
(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-28 20:15:05 +00:00
|
|
|
=========================================
|
|
|
|
Strings containing escape sequence
|
|
|
|
=========================================
|
|
|
|
|
|
|
|
echo "\"The great escape\`\${var}"
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program (command (command_name (word)) (string)))
|
|
|
|
|
2018-03-01 17:54:08 +00:00
|
|
|
======================================
|
|
|
|
Strings containing special characters
|
|
|
|
======================================
|
|
|
|
|
|
|
|
echo "s/$/'/"
|
|
|
|
echo "#"
|
2018-08-02 23:32:30 +00:00
|
|
|
echo "s$"
|
2018-03-01 17:54:08 +00:00
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
2018-08-02 23:32:30 +00:00
|
|
|
(command (command_name (word)) (string))
|
2018-03-01 17:54:08 +00:00
|
|
|
(command (command_name (word)) (string))
|
|
|
|
(command (command_name (word)) (string)))
|
2019-07-26 17:29:35 +00:00
|
|
|
|
|
|
|
========================================
|
|
|
|
Strings with ANSI-C quoting
|
|
|
|
========================================
|
|
|
|
|
|
|
|
echo $'Here\'s johnny!\r\n'
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program (command (command_name (word)) (ansii_c_string)))
|
2018-03-01 17:54:08 +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)))
|
2018-02-28 19:13:49 +00:00
|
|
|
(command (command_name (word)) (expansion (subscript (variable_name) (word))))
|
|
|
|
(command (command_name (word)) (expansion (subscript (variable_name) (word))))
|
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)))))))
|
2018-10-18 22:28:46 +00:00
|
|
|
|
|
|
|
==============================
|
|
|
|
Escaped characters in strings
|
|
|
|
==============================
|
|
|
|
|
|
|
|
echo -ne "\033k$1\033\\" > /dev/stderr
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
(program
|
|
|
|
(redirected_statement
|
|
|
|
(command (command_name (word)) (word) (string (simple_expansion (variable_name))))
|
|
|
|
(file_redirect (word))))
|