summaryrefslogtreecommitdiff
path: root/content/cheatsheets/bash.md
blob: a00ebb9d1743a16632701f4c5bf1631f5d9db9f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
+++
title = "bash"
disable_share = true
+++

# Test operators

## String

|op |                    desc                    |                  example                  |
|---|--------------------------------------------|-------------------------------------------|
|=  |is equal to                                 |`if [ "$a" = "$b" ]`                       |
|== |is equal to                                 |`if [ "$a" == "$b" ]`                      |
|!= |is not equal to                             |`if [ "$a" != "$b" ]`                      |
|<  |is less than, in ASCII alphabetical order   |`if [[ "$a" < "$b" ]]; if [ "$a" \< "$b" ]`|
|>  |is greater than, in ASCII alphabetical order|`if [[ "$a" > "$b" ]]; if [ "$a" \> "$b" ]`|
|-z |string is null, that is, has zero length    |`if [ -z "$a" ]`                           |

## Integer

| op  | desc                                                    | example                |
| --- | ------------------------------------------------------- | ---------------------- |
| -eq | is equal to                                             | `if [ "$a" -eq "$b" ]` |
| -ne | is not equal to                                         | `if [ "$a" -ne "$b" ]` |
| -gt | is greater than                                         | `if [ "$a" -gt "$b" ]` |
| -ge | is greater than or equal to                             | `if [ "$a" -ge "$b" ]` |
| -lt | is less than                                            | `if [ "$a" -lt "$b" ]` |
| -le | is less than or equal to                                | `if [ "$a" -le "$b" ]` |
| <   | is less than                                            | `(("$a" < "$b"))`      |
| <=  | is less than or equal to                                | `(("$a" <= "$b"))`     |
| >   | is greater than                                         | `(("$a" > "$b"))`      |
| >=  | is greater than or equal to                             | `(("$a" >= "$b"))`     |

## File

|   op    |                                       desc                                       |        example        |
|---------|----------------------------------------------------------------------------------|-----------------------|
|-h       |file is a symbolic link                                                           |`if [ -h "$f" ]`       |
|-L       |file is a symbolic link                                                           |`if [ -L "$f" ]`       |
|-S       |file is a socket                                                                  |`if [ -S "$f" ]`       |
|-t       |file (descriptor) is associated with a terminal device                            |`if [ -t "$f" ]`       |
|-r       |file has read permission (for the user running the test)                          |`if [ -r "$f" ]`       |
|-w       |file has write permission (for the user running the test)                         |`if [ -w "$f" ]`       |
|-x       |file has execute permission (for the user running the test)                       |`if [ -x "$f" ]`       |
|-g       |set-group-id (sgid) flag set on file or directory                                 |`if [ -g "$f" ]`       |
|-u       |set-user-id (suid) flag set on file                                               |`if [ -u "$f" ]`       |
|-k       |sticky bit set                                                                    |`if [ -k "$f" ]`       |
|-O       |you are owner of file                                                             |`if [ -O "$f" ]`       |
|-G       |group-id of file same as yours                                                    |`if [ -G "$f" ]`       |
|-N       |file modified since it was last read                                              |`if [ -N "$f" ]`       |
|f1 -nt f2|file f1 is newer than f2                                                          |`if [ f1 -nt f2 "$f" ]`|
|f1 -ot f2|file f1 is older than f2                                                          |`if [ f1 -ot f2 "$f" ]`|
|f1 -ef f2|files f1 and f2 are hard links to the same file                                   |`if [ f1 -ef f2 "$f" ]`|
|!        |"not" -- reverses the sense of the tests above (returns true if condition absent).|`if [ ! "$f" ]`        |