Shell Scripting
Shebang
Portable Shebang
Its not as simple as it looks
#!/usr/bin/env sh
Test
Boolean Test
IT_HAS_IPMI=true
[ "$IT_HAS_IPMI" = true ]
File tests
This section probably holds the most tests, I'll list them in some logical order. Since Bash 4.1, all tests related to permissions respect ACLs, if the underlying filesystem/OS supports them.
Operator syntax | Description |
---|---|
-a |
True if |
-e |
True if |
-f |
True, if |
-d |
True, if |
-c |
True, if |
-b |
True, if |
-p |
True, if |
-S |
True, if |
-L |
True, if |
-h |
True, if |
-g |
True, if |
-u |
True, if |
-r |
True, if |
-w |
True, if |
-x |
True, if |
-s |
True, if |
-t |
True, if file descriptor |
True, if |
|
True, if |
|
True, if |
String tests
Operator syntax | Description |
---|---|
-z |
True, if |
-n |
True, if |
True, if the strings are equal. | |
True, if the strings are not equal. | |
True if |
|
True if |
Arithmetic tests
Operator syntax | Description |
---|---|
True, if the integers are equal. | |
True, if the integers are NOT equal. | |
True, if the first integer is less than or equal second one. | |
True, if the first integer is greater than or equal second one. | |
True, if the first integer is less than second one. | |
True, if the first integer is greater than second one. |
Misc syntax
Operator syntax | Description |
---|---|
True, if |
|
True, if either |
|
! |
True, if |
( |
Group a test (for precedence). Attention: In normal shell-usage, the "(" and ")" must be escaped; use "(" and ")"! |
-o |
True, if the [[internals:shell_options |
-v |
True if the variable |
-R |
True if the variable |
Directory
There and Back Again
Command | Description |
---|---|
pushd /somewhere | going away |
*popd | coming back |
Output
echo -n "blah\nblah\tblah"