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

  • Home
  • SEARCH
  • 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
Below is my example script: <li><a <?php if ($_GET['page']=='photos' && $_GET['view']!==projects||!==forsale) { echo (href=\#\
For example: script.js: function functionFromScriptJS() { alert('inside functionFromScriptJS'); } iframe.html: <html> <head> <script language=Javascript
im looking for an example script. I saw one yesterday but for the life
I'm just beginning to learn python. I wrote an example script to test OOP
Does anyone have an example of script that can work reliably well across IE/Firefox
for example I have a script that needs to put it's parent directory on
For example if I add four (blank) script tasks A1, A2, B1, and B2,
Example taken from Mozilla's help page <script type="text/javascript"> re = /(\w+)\s(\w+)/; str = "John
Example: http://vincent-massaro.com/map/ Currently, the script allows you to click on a piece of the

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.