-
Notifications
You must be signed in to change notification settings - Fork 662
Expand file tree
/
Copy pathbash.sh
More file actions
71 lines (52 loc) · 1.53 KB
/
bash.sh
File metadata and controls
71 lines (52 loc) · 1.53 KB
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
#!/usr/bin/env bash
# This is a comment
readonly VAR1="Hello" # String literal
VAR2=42 # Integer literal
VAR3=$((VAR2 + 8)) # Arithmetic expansion
VAR4=$(echo "World") # Command substitution
function greet() { # Function definition
local name="$1" # Local variable, parameter expansion
echo "${VAR1}, $name! $VAR4" # String, parameter expansion, variable
}
greet "User" # Function call, string literal
if [[ $VAR2 -gt 40 && $VAR3 -eq 50 ]]; then # Conditional, test, operators
echo "Numbers are correct" # String literal
elif (( VAR2 < 40 )); then # Arithmetic test
echo 'VAR2 is less than 40' # Single-quoted string
else
echo "Other case"
fi
for i in {1..3}; do # Brace expansion, for loop
echo "Loop $i" # String, variable
done
case "$VAR4" in # Case statement
World) echo "It's World";; # Pattern, string
*) echo "Unknown";; # Wildcard
esac
arr=(one two three) # Array
echo "${arr[1]}" # Array access
declare -A assoc # Associative array
assoc[key]="value"
echo "${assoc[key]}"
# Here document
cat <<EOF
Multi-line
string with $VAR1
EOF
# Here string
grep H <<< "$VAR1"
# Subshell
(subshell_var=99; echo $subshell_var)
# Redirection
echo "Redirected" > /dev/null
# Background job
sleep 1 &
# Arithmetic assignment
let VAR2+=1
# Process substitution
diff <(echo foo) <(echo bar)
# Command grouping
{ echo "Group 1"; echo "Group 2"; }
# Escaped characters
echo "A quote: \" and a backslash: \\"
# End of file