How do variables, $?, $@ and $* behave?
Shell variables hold strings and have no strict typing. $? is the exit status of the last command, where 0 means success.
greeting="hello"
echo "${greeting} world"
readonly PI=3.14
export PATH="$PATH:/opt/bin"
${var:-default} supplies a default without assigning, ${var:=default} assigns it, ${#var} is the length and local scopes a variable to a function.
For positional parameters, the quoting rules are crucial:
"$@"expands to separate, individually quoted arguments, which is correct for forwarding."$*"joins all arguments into a single string using the first character ofIFS.$#is the argument count,$0the script name,$1and onward the arguments, andshiftdrops the first.
for arg in "$@"; do echo "$arg"; done
Always prefer "$@". Use readonly for constants, and arrays where "${arr[@]}" is the analogue of "$@".