| 1 | #!/bin/bash |
| 2 | |
| 3 | # Bash script example with syntax highlighting |
| 4 | |
| 5 | # Variables |
| 6 | NAME="Bash Script" |
| 7 | COUNT=10 |
| 8 | readonly CONSTANT="Can't change this" |
| 9 | |
| 10 | # Function definition |
| 11 | function greet() { |
| 12 | local name=$1 |
| 13 | echo "Hello, $name!" |
| 14 | } |
| 15 | |
| 16 | # Conditional |
| 17 | if [ -f "$0" ]; then |
| 18 | echo "This script exists: $0" |
| 19 | fi |
| 20 | |
| 21 | # Case statement |
| 22 | case "$1" in |
| 23 | start) |
| 24 | echo "Starting..." |
| 25 | ;; |
| 26 | stop) |
| 27 | echo "Stopping..." |
| 28 | ;; |
| 29 | *) |
| 30 | echo "Usage: $0 {start|stop}" |
| 31 | exit 1 |
| 32 | ;; |
| 33 | esac |
| 34 | |
| 35 | # Loops |
| 36 | for i in {1..5}; do |
| 37 | if [ $((i % 2)) -eq 0 ]; then |
| 38 | echo "$i is even" |
| 39 | else |
| 40 | echo "$i is odd" |
| 41 | fi |
| 42 | done |
| 43 | |
| 44 | # Array |
| 45 | declare -a fruits=("apple" "banana" "orange") |
| 46 | for fruit in "${fruits[@]}"; do |
| 47 | echo "Fruit: $fruit" |
| 48 | done |
| 49 | |
| 50 | # Command substitution |
| 51 | current_date=$(date +"%Y-%m-%d") |
| 52 | echo "Today is: $current_date" |
| 53 | |
| 54 | # Pipe and redirection |
| 55 | ls -la | grep "^d" > /tmp/directories.txt |
| 56 | |
| 57 | # Call function |
| 58 | greet "World" |