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 8811839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:27:39+00:00 2026-06-14T03:27:39+00:00

I’m kind of new in bash and I have a problem for which I’m

  • 0

I’m kind of new in bash and I have a problem for which I’m unable to find solution anywhere on the internet.

Before I do something like trying to program it by myself, I would like to see what is the standard way of dealing with situations like this.

Basicaly I would like to capture getopts to array…

I’m not sure if I’m clear enough so let’s try to take a look at this PHP example:

http://www.test.com/index.php?a=1&b=2

<?php
function getParams() {
return $_GET[];
}
?>

This is what I want for like:
myscript -a 1 -b 2

So far, each and every mention of getopts that i have found was implicating that I need to know exactly what I’m trying to get like:

while getopts a:b: option
do
  case "${option}"
    in
      a) a=${OPTARG};;
      b) b=${OPTARG};;
  esac
done

Is there a way in which I could have associative array of all arguments passed to the script no matter what those options are so I can always do something like this:

OPTS = get_params

echo $OPTS[@]

And then I could do some validation of my own etc…

Once again sorry if I’m missing something obvious, I’m really new to bash and I would really appreciate any help or guidance where to start.

Thank you very much in advance.

  • 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-14T03:27:40+00:00Added an answer on June 14, 2026 at 3:27 am

    You can go along these lines:

    #!/bin/bash
    
    declare -A my_opts
    
    while (($#)); do
       case $1 in
          -- )
             shift
             break
             ;;
          -* )
             my_opts[${1#-}]=$2
             shift 2
             ;;
          * )
             break
             ;;
       esac
    done
    
    for k in "${!my_opts[@]}"; do
       echo "$k => ${my_opts[$k]}"
    done
    
    # At this point, you still have the remaining arguments in $@
    
    echo "Arguments left: ${@:-(none)}"
    
    # End of script #
    

    Call this script test_args and chmod +x test_args. Then (illustrating the side effects of this oversimplified method):

    $ ./test_args -a 1 -b 2
    a => 1
    b => 2
    Arguments left: (none)
    
    $ ./test_args -a 1 -- -b 2
    a => 1
    Arguments left: -b 2
    $ ./test_args -a 1 foo -b 2
    a => 1
    Arguments left: foo -b 2
    
    $ ./test_args --a 1 -b 2
    -a => 1
    b => 2
    Arguments left: (none)
    
    $ # And a bad one (with this simplified version):
    $ ./test_args -a -- foo
    -a => --
    Arguments left: foo
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

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.