Variable Expansion and Quoting

Demonstrates variable expansion, quoting rules, and special variables in Bash including single vs double quotes and command-line arguments.

BashCli Tooling

Code

snippet
#!/usr/bin/env bash
set -euo pipefail

# Basic variable assignment
name="World"
greeting="Hello, $name!"
literal='No expansion: $name'

echo "$greeting"
echo "$literal"

# Special variables
echo "Script: $0"
echo "PID: $$"
echo "Exit status: $?"

# Command-line arguments
echo "Arg count: $#"
echo "All args: $@"
echo "First arg: $1"
echo "Args as string: $*"

# Default values
config_file="${CONFIG_FILE:-/etc/default.conf}"
echo "Config: $config_file"

# Indirect reference
var_name="greeting"
echo "Indirect: ${!var_name}"

# String length
echo "Name length: ${#name}"

# Substring extraction
full_path="/usr/local/bin/program"
echo "Substring: ${full_path:10}"
echo "Slice: ${full_path:0:4}"