|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Variable declaration |
| 4 | +var="Hello, World" |
| 5 | +number=42 |
| 6 | +array=("apple" "banana" "cherry") |
| 7 | + |
| 8 | +# Output variables |
| 9 | +echo "String variable: $var" |
| 10 | +echo "Number variable: $number" |
| 11 | +echo "Array variable: ${array[*]}" |
| 12 | + |
| 13 | +# If statement |
| 14 | +if [ "$number" -eq 42 ]; then |
| 15 | + echo "The number is 42" |
| 16 | +else |
| 17 | + echo "The number is not 42" |
| 18 | +fi |
| 19 | + |
| 20 | +# For loop |
| 21 | +for item in "${array[@]}"; do |
| 22 | + echo "Fruit: $item" |
| 23 | +done |
| 24 | + |
| 25 | +# While loop |
| 26 | +count=0 |
| 27 | +while [ $count -lt 3 ]; do |
| 28 | + echo "Count: $count" |
| 29 | + ((count++)) |
| 30 | +done |
| 31 | + |
| 32 | +# Function declaration |
| 33 | +my_function() { |
| 34 | + local local_var="Local Variable" |
| 35 | + echo "Function argument: $1" |
| 36 | + echo "$local_var from function" |
| 37 | +} |
| 38 | + |
| 39 | +my_function "Function Argument" |
| 40 | + |
| 41 | +# Case statement |
| 42 | +case "$number" in |
| 43 | +1) |
| 44 | + echo "Number is 1" |
| 45 | + ;; |
| 46 | +2) |
| 47 | + echo "Number is 2" |
| 48 | + ;; |
| 49 | +42) |
| 50 | + echo "Number is the answer to everything" |
| 51 | + ;; |
| 52 | +*) |
| 53 | + echo "Number is not recognized" |
| 54 | + ;; |
| 55 | +esac |
| 56 | + |
| 57 | +# Advanced constructs |
| 58 | +# Command substitution |
| 59 | +current_date=$(date) |
| 60 | + |
| 61 | +# Conditional execution |
| 62 | +ls non_existent_file || echo "File not found" |
| 63 | + |
| 64 | +# Here document |
| 65 | +cat <<EOF |
| 66 | +This is a |
| 67 | +multiline |
| 68 | +text block |
| 69 | +EOF |
| 70 | + |
| 71 | +# Arithmetic operations |
| 72 | +result=$((5 + 3)) |
| 73 | + |
| 74 | +echo "Arithmetic result: $result" |
| 75 | + |
| 76 | +# Variable expansion and substitution |
| 77 | +string="Hello, World" |
| 78 | +substring=${string:0:5} |
| 79 | +echo "Substring: $substring" |
| 80 | + |
| 81 | +# Command substitution within a string |
| 82 | +current_date="Current date is $(date)" |
| 83 | + |
| 84 | +# Advanced array operations |
| 85 | +declare -a arr=("apple" "banana" "cherry") |
| 86 | +length=${#arr[@]} |
| 87 | +echo "Array length: $length" |
| 88 | + |
| 89 | +# Complex if condition with logical operators |
| 90 | +if [[ "$string" == "Hello, World" && "$number" -eq 42 ]]; then |
| 91 | + echo "String and number match" |
| 92 | +else |
| 93 | + echo "No match" |
| 94 | +fi |
| 95 | + |
| 96 | +# C-style for loop |
| 97 | +for ((i = 1; i <= 5; i++)); do |
| 98 | + echo "C-style loop iteration: $i" |
| 99 | +done |
| 100 | + |
| 101 | +# Advanced function constructs |
| 102 | +recursive_function() { |
| 103 | + local value=$1 |
| 104 | + if [ $value -le 0 ]; then |
| 105 | + echo "Done" |
| 106 | + else |
| 107 | + echo "Value: $value" |
| 108 | + recursive_function $((value - 1)) |
| 109 | + fi |
| 110 | +} |
| 111 | + |
| 112 | +echo "Recursive Function:" |
| 113 | +recursive_function 5 |
| 114 | + |
| 115 | +# Subshell and variable scope |
| 116 | +(subshell_var="I'm in a subshell") |
| 117 | +echo "Subshell variable: $subshell_var" |
| 118 | +echo "Subshell variable outside the subshell: $subshell_var (should be empty)" |
| 119 | + |
| 120 | +# Command grouping |
| 121 | +{ |
| 122 | + echo "Command 1" |
| 123 | + echo "Command 2" |
| 124 | +} |
| 125 | +( |
| 126 | + echo "Command 3" |
| 127 | + echo "Command 4" |
| 128 | +) |
| 129 | + |
| 130 | +# Brace expansion |
| 131 | +echo "Brace Expansion: {1..5} => "{1..5} |
| 132 | + |
| 133 | +# Extended globbing |
| 134 | +shopt -s extglob |
| 135 | +files="file1.txt file2.txt file3.log" |
| 136 | +echo "Extended Globbing: ${files%%.*(txt|log)}" |
| 137 | + |
| 138 | +# Conditional execution and redirection |
| 139 | +false && echo "This won't execute" |
| 140 | +true || echo "This will execute" |
| 141 | + |
| 142 | +# Input redirection |
| 143 | +cat <input.txt |
| 144 | + |
| 145 | +# Process substitution |
| 146 | +diff <(sort file1.txt) <(sort file2.txt) |
| 147 | + |
| 148 | +# Multiline command |
| 149 | +long_command="This is a very long command that \ |
| 150 | +extends over multiple lines for readability." |
| 151 | +echo "$long_command" |
| 152 | + |
| 153 | +# Advanced parameter expansion |
| 154 | +path="/path/to/some/directory/" |
| 155 | +echo "Trimmed Path: ${path%/}" |
| 156 | + |
| 157 | +# Advanced arithmetic operations |
| 158 | +value=5 |
| 159 | +((value++)) |
| 160 | +echo "Incremented value: $value" |
| 161 | + |
| 162 | +# Array slicing |
| 163 | +numbers=(1 2 3 4 5) |
| 164 | +sliced_numbers=("${numbers[@]:1:3}") |
| 165 | +echo "Sliced array: ${sliced_numbers[*]}" |
0 commit comments