Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jimratliff/ddcb5eb7e6fb184840e5dfd7e240c7ee to your computer and use it in GitHub Desktop.
Save jimratliff/ddcb5eb7e6fb184840e5dfd7e240c7ee to your computer and use it in GitHub Desktop.
Example: Pass array to bash function without command substitution #bash #bash_exemplar
Context:
Pass array to function, where one or more elements of the array have embedded spaces.
Goal: Have the embedded spaces respected in the array processed by the function.
############### The script
#!/usr/bin/env bash
set -eu
function echo_passed_array() {
list_of_options=("$@")
echo "List of options [0]: ${list_of_options[0]}"
echo "List of options [1]: ${list_of_options[1]}"
echo "List of options [2]: ${list_of_options[2]}"
return 0
}
# Main
two_word_element="Maybe someday"
declare -a available_options
available_options[0]="Yes"
available_options[1]="No"
available_options[2]="$two_word_element"
echo_passed_array "${available_options[@]}"
############### The output
List of options [0]: Yes
List of options [1]: No
List of options [2]: Maybe someday
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment