-
Notifications
You must be signed in to change notification settings - Fork 0
/
bash_quick_help.txt
executable file
·108 lines (93 loc) · 2.51 KB
/
bash_quick_help.txt
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
Wildcards/Expansion:
*=.*
?=.
[SET],[!SET]=Any one of / none of, [a-z] [!a-c,w-z]
{s1,s2}=(s1|s2)
Handling Arguments:
$1, $2, $3, Args in order
$@ List of all args
String Operators:
${var:-word} If var exists and is not null returns it's value, otherwise
return word
${var:=word} If var exists and is not null return it's value, otherwise set
var to word and return it's value
${var:+word} If var exists and is not null return word, otherwise return null
${var:?message} If var exists and is not null return it's value, otherwise
displays "bash: $var:$message" and aborts the current script or command
${#string} String length
${var:offset[:length]} Returns substring of var from offset that's length long
Pattern matching
${var#pattern} Deletes the shortest match of pattern from the front of var
${var##pattern} Deletes the longest match of pattern form the front of var
${var%pattern} Deletes the shortest match of pattern from the end of var
${var%%pattern} Deletes the longest match of pattern form the end of var
${var/pattern/string} Replaces the longest match of pattern in var with
string, replaces only the first match
${var//pattern/string} Replaces the longest match of pattern in var with
string, replaces all matches
Condition test operators:
-d file, file exists and is a directory
-e file, file exists
-f file, file exists and is a regular file
-r file, file is readable
-s file, file exists and is not empty
-w file, file is writable
-x file, file is executable
-O file, you own file
-G file, your group owns file
file1 -nt file2, file 1 is newer than file 2
file1 -ot file2, file 1 is older than file 2
String comparisons:
str1 = str2, str1 matches str2
str1 != str2, str1 does not match str2
str1 < str2, str1 is less than str2
str1 > str2, str1 is greater than str2
-n str, str has length > 0
-z str, str has length = 0
Integer tests:
-eq, -ge, -gt, -le, -lt, -ne
Flow Control:
if "Condition"; then
Do something here
elif "Condition"
Do somethine else
else
Fall through
fi
for value in list; do
Statements using $value
done
while condition; do
something
done
until condition; do
something
done
Traditional for loop:
declare -i idx
idx=0
while [ $idx -lt 100 ]; do
echo $idx
idx=$idx+1
done
case expr in
pattern1)
do something;;
pattern2)
do somethine else;;
esac
select value [ in list ]; do
do something with $value as chosen by the user
done
Functions:
function funcName
{
do commands
}
#Invoke it:
funcName some args
Here-documents:
command << label
pass some lines directly
as input to command until
label