Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8630403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:03:08+00:00 2026-06-12T09:03:08+00:00

How do I convert command-line arguments into a bash script array? I want to

  • 0

How do I convert command-line arguments into a bash script array?

I want to take this:

./something.sh arg1 arg2 arg3

and convert it to

myArray=( arg1 arg2 arg3 )

so that I can use myArray for further use in the script.

This previous SO post comes close, but doesn’t go into how to create an array: How do I parse command line arguments in Bash?

I need to convert the arguments into a regular bash script array; I realize I could use other languages (Python, for instance) but need to do this in bash. I guess I’m looking for an “append” function or something similar?

UPDATE: I also wanted to ask how to check for zero arguments and assign a default array value, and thanks to the answer below, was able to get this working:

if [ "$#" -eq 0 ]; then
  myArray=( defaultarg1 defaultarg2 )
else
  myArray=( "$@" )
fi
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:03:09+00:00Added an answer on June 12, 2026 at 9:03 am

    Actually your command line arguments are practically like an array already. At least, you can treat the $@ variable much like an array. That said, you can convert it into an actual array like this:

    myArray=( "$@" )
    

    If you just want to type some arguments and feed them into the $@ value, use set:

    $ set -- apple banana 'kiwi fruit'
    $ echo "$#"
    3
    $ echo "$@"
    apple banana kiwi fruit
    $ for arg in "${@}"; do echo -n ", $arg"; done
    , apple, banana, kiwi fruit
    

    Understanding how to use the argument structure is particularly useful in POSIX sh, which has nothing else like an array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can I convert this into 1 command line on bash in sqlplus? cause i
Can anyone convert this curl command that works in command line to php code
In this answer to 'GhostScript command line parameters to convert EPS to PDF' ;
This program supposed to find command line arguments entered on Unix which ends with
I am in the process of writing a PHP command line script to convert
I have this code where I want to accept command line args fx 12
How is it possible to convert a script accepting a command-line argument to be
Loads of answers on how to do it for a command line convert /path/to/file/file.pdf[3]
Please suggest a small command-line utility (for Windows) to convert files from particular directory
The program I am working is passed 3 command line arguments, all of which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.