-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhello.sh
More file actions
executable file
·28 lines (23 loc) · 790 Bytes
/
Copy pathhello.sh
File metadata and controls
executable file
·28 lines (23 loc) · 790 Bytes
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
#!/usr/bin/env bash
# hello.sh — your first Bash script: variables, input, conditions, and loops.
# Usage: ./hello.sh [name]
# Pass a name as the first argument, or you'll be prompted for one.
set -euo pipefail
# Variable + a default so `set -u` doesn't error when no argument is passed.
name="${1:-}"
# Condition: if the name is empty, read one from standard input.
if [[ -z "$name" ]]; then
read -rp "Your name: " name
fi
# Command substitution: $(date +%F) is replaced by today's date.
echo "Hello, $name — today is $(date +%F)"
# for loop over a fixed list.
for i in 1 2 3; do
echo "count $i"
done
# while loop with a counter.
count=0
while [[ $count -lt 3 ]]; do
echo "while $count"
count=$((count + 1)) # avoids the ((count++)) exit-code trap under `set -e`
done