Home > bash > Bash: parsing arguments with ‘getopts’

Bash: parsing arguments with ‘getopts’

Today I was writing some scripts, and in every script I wanted something to handle all input arguments, in a good way, so I could pass my arguments in any order and my program would know about it.

I used ‘getopts’ before, but this time I decided to write some stuff here about it.

Let me show you how useful it can be:

Let’s suppose that I’m writing a test script, that needs, as argument, the type of the test, the server, the server root password and for debugging purpose we’re going to have a verbose flag too. So, putting it down:

  • “-t” – the type of the test, let’s suppose we have “test1″ and “test2″
  • “-s” – the server
  • “-p” – the root password of the server
  • “-v”- a flag just to let the script run in a verbose mode

Ok, now how we’re going to write this script and parse these arguments? We can use the harder way, fixing an order and parsing it by hand at the script, something like this:

salveti@evalap /tmp/scripts $ cat test_script.sh
#!/bin/bash
# Argument order = -t test -r server -p password -v
TEST=$2
SERVER=$4
PASSWD=$6
if [[ $# -gt 6 ]]
then
    VERBOSE=1
else
     VERBOSE=2
fi

Alright, this works, but if you want to run the script with the arguments in a different way? Or if you forget and put it in the right order? It’ll not work, so, this is an ugly solution.

Ok, but how can you deal with arguments not worrying about the order and if needs an argument or not? Getopts is the answer ;)

Let’s see how we can write the script using getopts and them we explain how it works.

The new script (it’s bigger, I’ll explain why):
#!/bin/bash
# Argument = -t test -r server -p password -v

usage()
{
cat << EOF
usage: $0 options

This script run the test1 or test2 over a machine.

OPTIONS:
   -h      Show this message
   -t      Test type, can be ‘test1′ or ‘test2′
   -r      Server address
   -p      Server root password
   -v      Verbose
EOF
}

TEST=
SERVER=
PASSWD=
VERBOSE=
while getopts “ht:r:p:v” OPTION
do
     case $OPTION in
         h)
             usage
             exit 1
             ;;
         t)
             TEST=$OPTARG
             ;;
         r)
             SERVER=$OPTARG
             ;;
         p)
             PASSWD=$OPTARG
             ;;
         v)
             VERBOSE=1
             ;;
         ?)
             usage
             exit
             ;;
     esac
done

if [[ -z $TEST ]] || [[ -z $SERVER ]] || [[ -z $PASSWD ]]
then
     usage
     exit 1
fi

In this script I created a usage function, just to help you explaining all arguments.

Then, we can see the getopts' call while getopts "ht:r:p:v" OPTION, this is the main point of the script, it's how we deal with arguments using getopts. Getopts require an optstring and a var name, just to help you checking the arguments.

When you call getopts, it will walk in your optstring argument, identifying which argument needs a value and which don't. After getting an argument, getopts set the OPTION var, so you can check it using a case code block, or something like that. If your argument needs a value, getopts will set the var $OPTARG with the value, so you can check and see if it's what you were expecting (in this example, check if the test argument is passed with "test1" or "test2"). Easy hã?

Ok, but what is this ":" doing in the arguments? And why the arguments "h" and "t" are together?

This is an import point of getopts. You can use ":" in two cases, one when you want getopts to deal with argument's errors, and another to tell getopts which argument needs a value.

First, the error checking. When you pass the arguments to getopts in the optstring, getopts will only check what's there, so if you pass an argument that's not listed at optstring getopts will give an error (because it's not a valid argument). When you put ":" at the beginning of the optstring, ":ht:r:p:v" for example, getopts sets the OPTION var with "?" and the $OPTARG with the wrong character, but no output will be written to standard error; otherwise, the shell variable $OPTARG will be unset and a diagnostic message will be written to standard error (./test_script.sh: illegal option -- l, if you pass the argument -l, for example).

Second, how to tell getopts which argument needs a value. When you need an argument that needs a value, "-t test1" for example, you put the ":" right after the argument in the optstring. If your var is just a flag, withou any additional argument, just leave the var, without the ":" following it.

So, in the example, you can see that I'm leaving the error checking to getopts, the vars "t", "r", "p" needs a value and "v" is just a flag.

To finish the script, we have a var checking, just to see if all vars that needs a value are not empty.

And, that's it. For now, you can try making a new script and playing with it a little, it's not so hard and can help you very much when writing new scripts :)

About these ads
Categories: bash
  1. January 9, 2008 at 9:03 am

    Great example. Helped a lot to me. Thank you.

  2. January 15, 2008 at 2:15 pm

    Very nice tutorial.

    I dont like the fact that with getopts your parameter names can only be 1 character long though

    btw if you do your tests like this : if [ -z "$TEST" ]
    then you dont need to initialize your variables to empty values.

  3. Andruk
    February 15, 2008 at 5:11 am

    I was looking for a good getopts page, this is very concise and well written.

    As Dieter_be also said, do you know of a way I can do long options too? Like:
    ./scriptname –help

    Any help would be greatly appreciated.

    Thank you.
    –Andruk

  4. jacques
    March 1, 2008 at 12:29 pm

    4 different articles on getopts + bash man pages. didn´t understand it.
    just skimmed you article and got it first time around. GREAT

  5. Matt
    March 3, 2008 at 9:23 am

    Thanks a lot!

  6. March 22, 2008 at 5:43 pm

    Apparently you cannot do long options with getopts. You can however with getopt.

    getopts is a bash builtin that wraps around getopt. More info here : http://aplawrence.com/Unix/getopts.html

  7. terry
    April 11, 2008 at 7:36 am

    how to get two paras from commond line? for example, “# Argument = -t test -r server -p username password -v”. for -p, how to get username first and then password? thank you.

  8. April 15, 2008 at 10:35 pm

    I don’t think that you can do this with getopts. Why don’t you just add another option for the password?

  9. alex
    April 18, 2008 at 12:50 am

    same situation as terry. Thats becoz that option, which is -p in terry’s case, needs two paras, if add another option for the passwd, that’d become two different options with two different paras, am i right?

  10. April 23, 2008 at 11:39 pm

    Yes, you’d probably have one param for the username and another for the password.

  11. Juergen
    May 7, 2008 at 5:13 pm

    Let’s say, you have an arbitrary number of hosts as parameters of an option – like: “myscript.sh -h host1 host2 host3 …”. No chance for getopts?

  12. rsalveti
    May 9, 2008 at 12:38 am

    @Juergen: getopts will get, by default, only the first host. What you could do is give all the hosts in one string, and then parse it at your code (“host1,host2,host3″ for example). It’s not a good solution, but depending on what you have could be an easy solution, at least :-)

  13. axm
    May 10, 2008 at 5:08 am

    I do not think it is not a good solution, just use space as divider and handle them in a for loop.

    for host in $OPTARG; do …

  14. axm
    May 10, 2008 at 5:25 am

    As it is pointed out at http://aplawrence.com/Unix/getopts.html,

    shift $((OPTIND-1)); OPTIND=1

    after parsing an option (discards parameter, resets option parsing index) leaves you with just the remaining non-option arguments in $*

  15. axm
    May 10, 2008 at 7:59 am
  16. Anonymous
    August 14, 2008 at 1:03 pm

    I’d like to know how to stop getopts using one of the other options as an argument to an option which needs an argument.
    e.g if you do test_script.sh -t -r it puts -r as $OPTARG for the -t option.
    Any ideas?

  17. rsalveti
    August 27, 2008 at 7:46 pm

    @Anonymous: the trick is the ‘:’ after the option you want.

    “You can use “:” in two cases, one when you want getopts to deal with argument’s errors, and another to tell getopts which argument needs a value.”

  18. November 20, 2008 at 3:09 pm

    Thanks! That came in handy :-)

  19. December 4, 2008 at 8:11 pm

    Thank you for your post. It helped me today with my bash script that seemed to follow your example very closely.

  20. Jack
    January 9, 2009 at 3:40 pm

    Thanks, very useful.

  21. Witsend
    February 2, 2009 at 10:38 am

    I have @Anonymous’s problem because if I have only one
    compulsory optarg option -t (“ht:v”) I cannot get around that:
    test_script.sh -t -h
    sets TEST to “-h” but provides no usage/error message.
    Alternatively, using your script try the silly test:
    test_script.sh -t -h -r -v -p -h
    which generates TEST, PASWORD, and SERVER set, but no
    usage/error message.

  22. rsalveti
    February 3, 2009 at 12:41 am

    @Witsend: don’t think getopts have anything to help you solving this problem :-(

    What you could do is to check the opt values before assuming they are correct.

  23. Witsend
    February 3, 2009 at 1:16 pm

    As I’ve discovered … and thanks.
    I used ‘if [[ "$OPTARG" =~ "regexp" ]] …’
    because my OPTARG was a regular expression,
    but this may not work for all folk.

  24. Witsend
    February 3, 2009 at 1:29 pm

    Whoops. Just noted bash 3.2 advice to leave
    off the quotes around regexp in the previous
    message. The reason is arcane … see
    https://bugs.launchpad.net/ubuntu-website/+bug/109931

  25. Jimmy
    February 9, 2009 at 12:31 pm

    Hi Rsalveti. very well presented explaination. thanks a million as I have been going through books and website to get this clarified.

    I have written a similar script as a test and I have a strange problem.

    ie. ./myscript -a test1 -b test2

    these parameters are acknowledged first time around but not thereafter.
    until I run the script without parameters. then the next time I use the parameters they are acknowlwdged.

    has anyone seen this problem in their scripts?

  26. Jack
    March 8, 2009 at 10:19 pm

    Thanks! Just what I needed. I wish I could understand the documentation for getopt so I could use long arguments, but this will do nicely.

    One note: the case statement matchers are just regular expressions, so if you want two options to have the same result (like h and ?) you can just do:

    h|\?)
    usage
    exit 1
    ;;

  27. Daniel
    March 26, 2009 at 5:45 pm

    Hey, quit reducing the contrast (using gray, instead of white, on black). Don’t make the text harder to read.

  28. Lical
    April 10, 2009 at 12:47 pm

    A small step for most, a giant leap for me. My shell script abilities made a quantum jump… (from 0 to 1 I’m afraid ;-)

  29. Sebastian
    May 15, 2009 at 2:17 pm

    tank you VERY MUCH,

    just in case, also:

    http://www.linuxcommand.org/wss0130.php (useful for long options)

  30. Rishi
    August 12, 2009 at 8:44 am

    What if i want to have three arguements to any particular option

    like -a can have only three

    -a foo goo bar //how can i do this

    mail me the answer if possible
    rishi.b.agrawal@gmail.com

  31. Jimmy
    August 12, 2009 at 8:54 am

    how about

    myfunction -s 10 -a “foo goo bar”

    then split the foo, goo and bar afterwards?

  32. DEGRELLE
    September 19, 2009 at 11:00 pm

    Thanks for this clear and succinct posting. It certainly helped me out.

  33. azimuth
    September 24, 2009 at 6:23 pm

    You rock! Thanks for posting this. I was quickly up and runing using your instructions.

    One thing I might clarify for others is that you need to put the colon after the switch letter in the optstring for any argument that needs a value…not just those that require the argument to be passed. I thought you meant the latter and omitted the colon for arguments that I wanted to be treated as option rather than required. That didn’t work. They’re made optional by defaulting them to something and not throwing an error if they’re not passed or if they’re blank., rather than by omitting the colon. The colon is necessary for any argument that is not just a flag.

  34. Anonymous
    November 13, 2009 at 12:24 am

    What can I say, but thank you!

  35. April 12, 2010 at 3:46 pm

    EXCELLENT article. Was trying to find a solution to this exact conundrum. Nailed it !

  36. sharon
    July 29, 2010 at 5:04 pm

    Thank you..seriously you are the only one who helped..
    not even my prof :(
    thank you so much :)

  37. bc
    August 24, 2010 at 8:37 pm

    Very clean, very clear! Thanks

  38. jay
    November 12, 2010 at 9:58 pm

    Thank you very much for this!

  39. April 6, 2011 at 8:58 pm

    Somehow, “smart quotes” seem to have crept into the getopts code example; thankfully, Emacs alerted me to this by getting confused about what encoding to use for the file.

    Perhaps this is related to the fact that this example isn’t in a code element like the other one is? (Admittedly, the other one does not appear to use quotation marks at all, so it’s not certain that this would help, but it seems worth a try…)

    • April 6, 2011 at 9:03 pm

      Test: does while getopts "ht:r:p:v" OPTION have smart quotes?

  40. April 8, 2011 at 10:12 am

    Whenever I write a bash script, and its time to do it right, I end up looking for just this post.

    Just refreshed my memory once again, so thanks Ricardo.

  41. Anonymous
    April 26, 2011 at 4:56 pm

    Good article !
    i have the following scenario want to run the following script with manadory and optional argumnets

    Manadory options are :

    filename=””
    port=””
    optional arguments

    type -t [A/B]
    balances -b bal
    prices -p

    ./test filename port -t A -b bal

    my code i have that won’t parse the options is
    This is what i have
    #!/bin/bash
    filename=””
    port=””

    while getopts b:t:p opt
    do
    case $opt in
    b) BAL=”${OPTARG}”
    if [ "$BAL" == "" ]
    then
    print_usage $0
    else
    echo “Balances is ” $BAL
    fi ;;

    t) TYPE=”$OPTARG”;;
    p) PRICES=”$OPTARG”;;
    ?) print_usage $0
    exit 1;;
    esac
    done

    echo “Balances: “$BAL
    echo “Type: “$TYPE

    Any ideas why this won’t work for me i want to be able to pass the manatory options first and then the optional ?????

    • Andre
      October 7, 2011 at 12:38 pm

      I also need this to work:

      (Combination of switches and mandatory fields)

      ./test.sh -x switch1 -y switch2 -z switch3 mandatory_string1 mandatory_string2

      mandatory_string1 and 2 can no longer be picked up as $[0-9]. So how do we match it?

  42. May 19, 2011 at 7:03 am

    Thanks for the descriptive tutorial.

  43. Anonymous
    May 24, 2011 at 7:56 am

    Please note that the line:

            ?)

    should in fact say:

            "?")

    Otherwise it will match any one-letter parameter. In your example it doesn’t matter, but in a slightly different case, it may.

  44. Arvid Requate
    October 7, 2011 at 7:34 am

    The bash builtin getopts function can be used to parse long options by putting a dash character followed by a colon into the optspec. For an example script see:

    http://stackoverflow.com/questions/402377/using-getopts-in-bash-shell-script-to-get-long-and-short-command-line-options/7680682#7680682

  45. Anonymous
    November 18, 2011 at 1:54 pm

    Thank you! This is exactly what I was looking for. =]

  46. Florin
    November 18, 2011 at 4:45 pm

    Thanks a lot! Great help

  47. November 23, 2011 at 4:47 pm

    good explanation, thanks

  48. January 13, 2012 at 11:16 am

    Awesome – saved me a lot of time and frustration – thanks!!

  49. Desdemona
    November 23, 2012 at 4:27 pm

    thanks, that was very helpful.
    have a nice day. :)

  50. December 19, 2012 at 3:59 am

    In your case statement use backslash-Question-mark, not solely Question-mark.

    Use
    case $name in
    \?) echo something;;
    esac

    Don’t use
    case $name in
    ?) echo something;;
    esac

  51. Anonymous
    January 24, 2013 at 7:41 pm

    Awesome tutorial tyty

  52. Anonymous
    January 25, 2013 at 9:19 am

    Very nice and helpful. You are being bookmarked!

  53. Vaibhav
    February 22, 2013 at 4:56 pm

    Thanks a bunch !!!

  54. Anonymous
    March 7, 2013 at 9:45 am

    Very useful tutorial. Thanks a lot!

  55. Anonymous
    March 24, 2013 at 3:02 pm

    Thanks for such a clear explanation!

  56. Anonymous
    June 25, 2013 at 12:32 pm

    Thank you very much

  57. Ramesh Mogadala
    September 5, 2013 at 4:09 pm

    Thank you very much for the very useful tutorial. I have similar issue, I need your help to resolve it

    USAGE=”$0 [-D -S -U -P -F -E [Log Errors To File] [-R ] [-G ] ”

    while getopts D:EeF:G:lLP:R:S:U:s: OPTION
    do
    case $OPTION in
    D) DATABASE=$OPTARG;;
    E) ERROR_LOG=1;;
    F) DUMP_FILE=$OPTARG;;
    G) RPT_GROUP=$OPTARG;;
    L) LOG=1;;
    l) LOAD_STATUS=1;;
    P) PASSWORD=$OPTARG;;
    R) RPT_USER=$OPTARG;;
    S) SERVER=$OPTARG;;
    s) SEVERITY=$OPTARG;;
    U) USERID=$OPTARG;;
    e) EMAIL_OPT=1;;
    ?) echo “USAGE:$USAGE “All Parameters are not passed properly so the job did not run at all” ”
    exit 1;;
    esac
    done

    I do have this in my script and I am trying to run the script as below I am getting below error because there is space between -e and -s but I want this to write a log file than just writing it to nohup.out, how to do this. Can you please help me with that.

    nohup /cygdrive/d/scripts/DB_Restore/db_restore_cat_test.bash -DFWWFB_M_DS -Sosdbabs4 -L -E -F “e:/Remote_Backups/daily/fwwfbts1/FWWFB_M_DS.1.bak” -e-s WARNING -PbcpM0m0n3y &

    $ more nohup.out
    Starting bashrc
    /cygdrive/d/scripts/DB_Restore/db_restore_cat_test.bash: illegal option — -
    USAGE:/cygdrive/d/scripts/DB_Restore/db_restore_cat_test.bash [-D -S -U -P -F -E [Log Errors To File] [-R ] [-G ] All Parameters are not passed properly so the job did not run at all

  58. Anonymous
    January 17, 2014 at 8:29 pm

    Just wanted to say, this was one of the clearest, most simple getops tutorial I’ve seen on the net. Appreciate you taking the time to write this.

  59. herison
    February 16, 2014 at 4:27 pm

    You can use bash paramparser library on github

    source paramparser.sh

    optAdd ‘test_type’ ‘TEST_TYPE’ ‘can be ‘test1′ or ‘test2′’ ‘test1′ #Default is test1
    optAdd ‘addr’ ‘SERV_ADDR’ ‘Server address’ ”
    optAdd ‘pwd’ ‘PWD’ ‘Server root password’ ”
    optAddFlag ‘verbose’ ‘A flag just to let the script run in a verbose mode.’

    optParse “$@”

    echo $test_type
    echo $addr
    echo $pwd
    echo $verbose

  60. Anonymous
    February 21, 2014 at 5:40 pm

    Great help page! thanks!

  61. Dave R
    March 6, 2014 at 2:55 pm

    Good article.
    What does -z does in “if [[ -z $TEST ]] || [[ -z $SERVER ]] || [[ -z $PASSWD ]]”?

  62. April 22, 2014 at 4:31 pm

    Just rastafarise the code jah make it work

  63. May 27, 2014 at 6:28 pm

    It’s actually very complicated in this active life to
    listen news on TV, so I just use the web for that reason, and get the latest news.

  64. May 30, 2014 at 1:51 am

    It’s actually a nice and helpful piece of info. I’m glad
    that you just shared this useful info with us.
    Please keep us up to date like this. Thank you for sharing.

  65. May 30, 2014 at 11:11 pm

    This article is really a good one it helps new thhe web users, who are wishing
    in favor of blogging.

  66. Johng440
    June 2, 2014 at 6:40 pm

    I discovered your blog site on google and test a number of of your early posts. Proceed to keep up the excellent operate. I simply extra up your RSS feed to my MSN Information Reader. Looking for forward to reading extra from you in a while! ffecfeggfbbc

  67. Om Sachdev
    June 24, 2014 at 11:09 am

    Super helpful.
    Internet blogs full of crap material. I was trying to find one good practical example and all I could found was theory and some crap examples.
    and then I found you page and put my search to an end.

    Thank you!

  68. July 7, 2014 at 7:49 pm

    After looking over a few of the articles on your blog,
    I honestly appreciate youjr way of blogging.
    I added it to my bookmark webpage list and will be checkibg
    back soon. Please check out my website too and let me know hoow you feel.

  69. July 17, 2014 at 7:44 am

    I like reading through an article that can ake men and women think.
    Also, thank you for allowing for mme to comment!

  1. June 6, 2011 at 5:53 pm
  2. July 14, 2011 at 5:34 pm
  3. July 10, 2012 at 1:50 am
  4. March 4, 2013 at 8:12 pm
  5. June 22, 2014 at 11:16 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: