How do you save all the arguments in a bash script to an array and print them individually?
1 Answer
Initialize the array:
ARGS=("$@") # "$@" gives the arguments passed to the script
ARGS=(arg1 arg2 arg3) # or fill the array out yourself
Display the array items:
for ARG in "${ARGS[@]}"; do
printf '%s\n' "$ARG"
done
1 Comment
glenn jackman
+1. Can also use a C-like for loop to iterate over the indices:
for (( i=0; i < ${#ARGS[@]}; i++ )); do printf "%d\t%s\n" $i "${ARGS[i]}"; done