3

How do you save all the arguments in a bash script to an array and print them individually?

1 Answer 1

6

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
Sign up to request clarification or add additional context in comments.

1 Comment

+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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.