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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:24:50+00:00 2026-05-26T00:24:50+00:00

Example of script usage ./myscript –p 1984 –n someName #!/bin/bash while getopts :npr opt

  • 0

Example of script usage

./myscript --p 1984 --n someName

#!/bin/bash

while getopts :npr opt 
do
   case $opt in
     n ) echo name= ???                ;;
     p ) echo port=  ???               ;;
     r ) echo robot= "Something"       ;;
     ? ) echo  "Useage: -p [#]"        ;;
  esac
done

How to I access the argument following the command option?

Moreover, if i type: ./myscript --p 1985 I would like to know how ot echo 1985 back and work with that argument.

  • 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-05-26T00:24:50+00:00Added an answer on May 26, 2026 at 12:24 am

    In bash, see help getopts: “When an option requires an argument, getopts places that argument into the shell variable OPTARG.”

    usage() { echo "Usage: $(basename $0) -n name -p port -r"; exit; }
    
    while getopts :n:p:r opt   # don't forget the colons for opts that take an arg
    do
       case $opt in
         n ) name="$OPTARG" ;;
         p ) port="$OPTARG" ;;
         r ) robot=chicken  ;;
         ? ) usage ;;
      esac
    done
    shift $(( OPTIND - 1 ))
    
    echo "the name is $name"
    echo "the port is $port"
    

    I’m sure you could google around for a solution to parse options in bash. Here’s a couple minutes’ effort:

    #!/bin/bash
    
    usage() { echo foo; exit; }
    
    while [[ $1 == -* ]]; do
      case "$1" in 
        --) shift 1; break ;;
        -p|--p|--port) port="$2"; shift 2;;
        -n|--n|--name) name="$2"; shift 2;;
        *) echo "unknown option: $1"; usage;;
      esac
    done
    
    echo "the name is $name"
    echo "the port is $port"
    echo "the rest of the args are:"; ( IFS=,; echo "$*" )
    

    And a test,

    $ bash longopts.sh --port 1234 --bar a b c
    unknown option: --bar
    foo
    $ bash longopts.sh --port 1234 a b c
    the name is
    the port is 1234
    the rest of the args are:
    a,b,c
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple script named example : #!/bin/sh echo $'${1}' Please note that
I have found following working example of CGI with bash. If I change first
I have some problems with a PHP script that calls a Bash script.... in
I want to run a shell script with this usage: ./run A.txt B.xml A.txt
Can any one explain actual usage of run script in xcode 4. what is
Writing a TCL script to match everything in between two key words Example: X509v3
Special documentation blocks in Python in http://www.doxygen.nl/manual/docblocks.html gives an example of doxygen usage with
Hi I'm looking for a quick example of type class usage in Scala that
Can someone show me an example how to use getopts properly or any other
First off, my knowledge of bash usage and syntax is very limited. Everything I've

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.