bash 

Send to Kindle
home » snippets » bash


Pages
completion        
printf        
read        



Notes

# Declare an array.
# If the array already existed, this is a nop and any
# elements in it stay there.
declare -a names

# Declare and initialize an array.
# If the array already existed, this assigns new
# elements to it destroying what was already there.
# In this specific case, it becomes an empty array.
declare -a names=()

# "${names[@]}" vs. "${names[*]}"
# This is like the difference between $@ and $*.
# Both need to have quotes around them.
# @ means get the whole array
# * means get the whole array as a single string.
print_args ${names[@]} # Receives 0 args.
print_args ${names[*]} # Receives 1 args.

# Append to array.
names+=(alice)
names+=(barbie)
print_args ${names[@]} # Receives 2 args.
print_args ${names[*]} # Receives 1 arg – "alice barbie"


# Indirect references / delayed expansion.
E=EDITOR
echo ${!E}     # echo's vim/emacs

foo() {
  echo Positional parameters: num=$#
  for ((i = 0; i < $#; ++i)) ; do
    echo $i: ${!i}
  done
}