bash
This code reads user inputs for name
and age
, then checks if name
is "Steve" and age
is 15 using a compound if
statement. It demonstrates handling multiple conditions in Bash.
read name read age if [[ "$name" == "Steve" ]] && [[ "$age" -eq 15 ]]; then echo "This will run if $name is Steve AND $age is 15." fi
bash internalflow controltests (conditions)compound condition
bash
This configures Bash shell options for safer scripting by preventing file overwriting, exiting on errors, revealing pipeline failures, and exposing unset variables.
set -o noclobber # Avoid overlay files (echo "hi" > foo) set -o errexit # Used to exit upon error, avoiding cascading errors set -o pipefail # Unveils hidden failures set -o nounset # Exposes unset variables
bash internalshell configuration and customizationstrict mode
bash
This demonstrates the usage of a here-document (heredoc) in Bash, including basic usage, variable expansion, and suppressing variable expansion.
cat <<EOF This is line 1. This is line 2. This is line 3. EOF ## Variable Expansion: name="Alice" cat <<EOF Hello, $name! EOF ## Suppressing Variable Expansion: name="Alice" cat <<'EOF' Hello, $name! EOF
bash internalfile and stream operationscontent operationshere-document (heredoc)
bash
This code uses a regular expression to extract an email address from a string, demonstrating regex-based pattern matching and extraction in Bash.
string="Hello, my email is user@example.com" # Use regex to match an email address if [[ $string =~ [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,} ]]; then echo "Email found: ${BASH_REMATCH[0]}" else echo "No email found." fi
bash internaldata manipulationsstring manipulation and expansionsregular expressions (regex)