Argument and command separators
Metacharacter | Meaning |
Blank | Argument separators, depending on the value of the IFS variable |
Newline | End of command |
| Pipe symbol |
| End of command |
| End of command; a command terminated with this symbol is run as a |
| ORIF; the next command is executed only if the preceding command |
| ANDIF; the next command is executed only if the preceding command |
Command grouping
Metacharacter | Meaning |
(command_list) | Execute command_list in a subshell |
{ command_list;} | Collect the output of all commands from command_list |
Execute command and replace with output
Metacharacter | Meaning |
`command` | Replace command with its output |
$(command) | Replace command with its output |
File name generation
Metacharacter | Meaning |
* | As a pattern on its own: As part of a pattern: |
? | As a pattern on its own: As part of a pattern: |
[s] | Replaced by exactly one of the characters which are not contained in the string |
[c1-c2] | Replaced by any one character from the range between c1 and c2 inclusive to |
[!s] | Replaced by exactly one of the characters which are not contained in the string |
[!c1-c2] | Replaced by exactly one of the characters which do not lie in the range c1 to c2 |
Redirection of standard output
Metacharacter | Meaning |
[n]> file | Redirect standard output [or file descriptor n] to file, deleting original contents |
[n]>> file | Redirect standard output [or file descriptor n] to file, retaining original contents |
[n]> &digit | Redirect standard output [or file descriptor n] to the file associated with file |
[n]> &- | Close standard output [or file descriptor n] |
Redirection of standard input
Metacharacter | Meaning |
[n] <file | Redirect standard input [or file descriptor n] to file |
[n] <<argument | Start a "here" document or redirect to file descriptor n |
[n] <<-argument | Start a "here" document or redirect to file descriptor n; stripping leading tabs |
[n] <&digit | Redirect standard input to the file [or file descriptor n] associated with file |
[n] <&- | Close standard input [or file descriptor n] |
[n] <>file | file [or file descriptor n] is opened as the standard input for reading and writing |
Shell variables and parameters
Metacharacter | Meaning |
name=value | Assign a value to the variable name |
$name | Value of the variable name; keyword parameter |
${name} | Like $name; the braces separate the variable name from following |
${name-default_value} | Replace with default_value if name is undefined |
${name=default_value} | Assign default_value if name is undefined |
${name?default_value} | Shell terminates process execution with error message |
${name+default_value} | Replace with the null string if name is undefined; |
${name:-default_value} | Replace with default_value if name is undefined or if its value is the |
${name:=default_value} | Assign default_value if name is undefined or if its value is the null |
${name:?default_value} | Shell terminates process execution with error message |
${name:+default_value} | Replace with the null string if name is undefined or if its value is the |
${#name} | Replace with the length of the string formed by name. If name is one |
${name%pattern} | If the POSIX shell pattern pattern matches the end of the value of |
${name%%pattern} | If the POSIX shell pattern pattern matches the end of the value of |
${name#pattern} | If the POSIX shell pattern pattern is to match the start of the value |
${name##pattern} | If the POSIX shell pattern pattern is to match the start of the value |
$0 | 0th command-line argument, i.e. the name of the command, the |
$1, $2, ... , $9 | Positional parameters |
$* | All command-line arguments |
$@ | All command-line arguments |
$# | Number of command-line arguments, excluding $0 |
$$ | Process ID (PID) of the current shell |
$! | PID of the last command run in the background |
$? | Exit status of the last executed command that was not run in the |
$- | All options set in the current shell |
Shell functions
Metacharacter | Meaning |
name() { command_list;} | Shell function; when name is invoked, the commands from |
Quoting metacharacters
Metacharacter | Meaning |
\ '...' "..." | Escapes the following metacharacter Escapes all metacharacters between quotes; treat as a single argument Does not escape the metacharacters $ `...` \ |
Miscellaneous
Metacharacter | Meaning |
# ;; | Comment character in shell scripts Command list terminator within case constructs |
~ | Tilde substitution |
(( )) | Arithmetic evaluation |
[[ ]] | Conditional expressions |