|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# This is a comment |
| 4 | + |
| 5 | +readonly VAR1="Hello" # String literal |
| 6 | +VAR2=42 # Integer literal |
| 7 | +VAR3=$((VAR2 + 8)) # Arithmetic expansion |
| 8 | +VAR4=$(echo "World") # Command substitution |
| 9 | + |
| 10 | +function greet() { # Function definition |
| 11 | + local name="$1" # Local variable, parameter expansion |
| 12 | + echo "${VAR1}, $name! $VAR4" # String, parameter expansion, variable |
| 13 | +} |
| 14 | + |
| 15 | +greet "User" # Function call, string literal |
| 16 | + |
| 17 | +if [[ $VAR2 -gt 40 && $VAR3 -eq 50 ]]; then # Conditional, test, operators |
| 18 | + echo "Numbers are correct" # String literal |
| 19 | +elif (( VAR2 < 40 )); then # Arithmetic test |
| 20 | + echo 'VAR2 is less than 40' # Single-quoted string |
| 21 | +else |
| 22 | + echo "Other case" |
| 23 | +fi |
| 24 | + |
| 25 | +for i in {1..3}; do # Brace expansion, for loop |
| 26 | + echo "Loop $i" # String, variable |
| 27 | +done |
| 28 | + |
| 29 | +case "$VAR4" in # Case statement |
| 30 | + World) echo "It's World";; # Pattern, string |
| 31 | + *) echo "Unknown";; # Wildcard |
| 32 | +esac |
| 33 | + |
| 34 | +arr=(one two three) # Array |
| 35 | +echo "${arr[1]}" # Array access |
| 36 | + |
| 37 | +declare -A assoc # Associative array |
| 38 | +assoc[key]="value" |
| 39 | +echo "${assoc[key]}" |
| 40 | + |
| 41 | +# Here document |
| 42 | +cat <<EOF |
| 43 | +Multi-line |
| 44 | +string with $VAR1 |
| 45 | +EOF |
| 46 | + |
| 47 | +# Here string |
| 48 | +grep H <<< "$VAR1" |
| 49 | + |
| 50 | +# Subshell |
| 51 | +(subshell_var=99; echo $subshell_var) |
| 52 | + |
| 53 | +# Redirection |
| 54 | +echo "Redirected" > /dev/null |
| 55 | + |
| 56 | +# Background job |
| 57 | +sleep 1 & |
| 58 | + |
| 59 | +# Arithmetic assignment |
| 60 | +let VAR2+=1 |
| 61 | + |
| 62 | +# Process substitution |
| 63 | +diff <(echo foo) <(echo bar) |
| 64 | + |
| 65 | +# Command grouping |
| 66 | +{ echo "Group 1"; echo "Group 2"; } |
| 67 | + |
| 68 | +# Escaped characters |
| 69 | +echo "A quote: \" and a backslash: \\" |
| 70 | + |
| 71 | +# End of file |
0 commit comments