Redirection Operators |
These operators redirect:
standard input (0), standard output (1) and standard error (2). |
||
> | redirects standard output to a file | ps aux > file_name | Creates a file and writes standard output. Clobbers any existing file. |
>> | appends standard output to a file | (date;who)>>file_name | Appends standard output to an existing file |
< | redirects standard input from a file | mail root<file_name | Redirects file to body of mail sent to root |
<< | redirects standard input to standard output until key_word appears | <<key_word | There must be no space between << and key_word. |
<&digit | Use file descriptor digit as standard input. | ||
&digit> | Use file descriptor digit as standard output. | ||
<&- | Close standard input. | ||
>&- | Close standard output. | ||
test Operators |
Used with the command test or the equivalent [ character.This is not an exhaustive list. See man test for all options.
|
||
Operator | Meaning | Number of Operands | |
[ -n A]
|
A is non-zero length | 1 | |
[ -z A ]
|
A is zero length | 1 | |
[ -d A ]
|
a directory exists named A | 1 | |
[ -f A ]
|
a file exists named A | 1 | |
[ -r A ]
|
a file or directory exists named A, and it is readable | 1 | |
[ -w A ]
|
a file or directory exists named A, and it is writable | 1 | |
[ -x A ]
|
a file or directory exists named A, and it is executable | 1 | |
[ 1 -eq 1 ]
|
the operands are integers and they are equal | 2 | |
[ 1 -ne 2 ]
|
the opposite of -eq | 2 | |
[ A = B ]
|
the operands are equivalent strings | 2 | |
[A != B ]
|
opposite of = | 2 | |
[ 2 -lt 3 ]
|
operand1 is strictly less than operand2 (both operands must be integers) | 2 | |
[ 3 -gt 2 ]
|
operand1 is strictly greater than operand2 (both operands must be integers) | 2 | |
[ 3 -ge 3 ]
|
operand1 is greater than or equal to operand2 (both operands must be integers) | 2 | |
[ 3 -le 4 ]
|
operand1 is less than or equal to operand2 (both operands must be integers) | 2 | |
[ A -eq B -o A -eq C ]
|
OR – either condition (on either side of -o) is true | ||
[ A = B -a A = C ]
|
AND – both conditions (on either side of -a) are true | ||
[ ! A = B ]
|
NOT – reverses the sense of any test operator | ||
File name (shell) wildcards |
These are expanded by the shell (not by programs called by the shell). Use these anywhere you’d expect to see a file name.
|
||
? | matches any single character | Chap? | matches Chap1, ChapA, etc. |
* | matches any string, zero-length or longer | Chap* | matches Chapter One, Chap1, ChapX |
[criteria] | matches any SINGLE character specified in [ ] | [abcXYZ] | matches a, b, c, X, Y or Z |
[!criteria] | matches any SINGLE character NOT in the [ ] | [!abcXYZ] | matches anything BUT a, b, c, X, Y or Z |
Some examples using the ls command: ls myfile[abc] ls myfile[a-z] ls myfile[a-eABCDE] ls myfile[!a-eABCDE] ls myfile[*?] When an expression containing these characters occurs in the middle of a command, bash substitutes the list of all files with names that match the pattern. This is known as globbing. These are used in “case” statements and “for” statements. When a glob begins with * or ?, it does not match files that begin with a dot. To match these, you need to specify the dot explicitly (e.g., .* or /tmp/.*). Under DOS, the pattern *.* matches every file. In sh, it matches every file that contains a dot. |
|||
These character-matching expressions are used by many commands (for instance grep) and script languages (like perl). NOTE THAT many of these are the same characters used for Shell Wildcards. They DO NOT have the same meaning here! |
|||
.
|
matches any single character | Notice the difference between this and the dot in tests above! | |
\? |
matches the preceding expression zero or one times | ||
*
|
matches the preceding item, zero or more times | There must be a character or wildcard before this character, in order for it to match.
Note that you can have 0 matches, and still “match.” |
|
+
|
matches the preceding item, one or more times | This character forces at least 1 match. | |
^
|
matches the beginning of a line | ^T | matches a line beginning in capital T |
$
|
matches the end of a line | Contents$ | matches the line: Table of Contents |
[ ]
|
matches any ONE of the characters enclosed | [ABC] [a-z] [^…] |
matches A or B or C matches any lowercase letter matches any char NOT in the list |
.*
|
matches zero or more instances of any character | .* e.*e |
used to match an entire line of text, or all text between two strings |
expr\{n\}
|
matches expr n times | test{3} | Only “testtesttest” matches |
expr\{min,max\}
|
matches expr from min to max times | [a-z]\{7\} [A-Z]\{1,10\} |
matches exactly 7 letters matches 1-10 times |
\(expr\)reg
|
matches expr and stores it in register reg | ||
( …\| … )
|
matches either of two strings | (mom\|pop) | matches either “mom” or “pop” |
See this extremely useful site for more about Regular Expressions: http://www.regular-expressions.info/reference.html |
|||
|
|||
‘
|
Single Quotes
|
Disable recognition of all special characters
|
|
”
|
Double Quotes
|
Protect most special characters, but allow for variable and command substitution (e.g., echo “today is `date`”)
|
|
\
|
Backslash: “Whack”
|
Escapes any special meaning of the next character
|
|
`
|
Backquote: “Tick”
|
Used for command substitution (e.g., HERE=`pwd`)
|
|
$
|
Dollar sign
|
Variable substitution character
|
|
Shell Argument Variables | |||
$#
|
The number of arguments passed with a command, e.g. |
||
$*
|
Holds all arguments passed with a command; |
||
$@
|
Holds the array of all arguments passed with a command:
quotes are eliminated but still delimit arguments! So arg1 arg2 “arg3 arg4” becomes: arg1 arg2 arg3 arg4 which looks like 3 arguments, and is! |
||
$?
|
A special variable that holds the (numeric) result (i.e. error code) for the last command executed. If the command succeeds, this value is 0.
|