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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:06:22+00:00 2026-05-23T02:06:22+00:00

Is there a way to feed non positional arguments to a shell script? Meaning

  • 0

Is there a way to feed non positional arguments to a shell script?
Meaning explicitly specify some kind of flag?

. myscript.sh value1 value2
. myscript.sh -val1=value1 -val2=value2
  • 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-23T02:06:23+00:00Added an answer on May 23, 2026 at 2:06 am

    You can use getopts, but I don’t like it because it’s complicated to use and it doesn’t support long option names (not the POSIX version anyway).

    I recommend against using environment variables. There’s just too much risk of name collision. For example, if your script reacts differently depending on the value of the ARCH environment variable, and it executes another script that (unbeknownst to you) also reacts to the ARCH environment variable, then you probably have a hard-to-find bug that only shows up occasionally.

    This is the pattern I use:

    #!/bin/sh
    
    usage() {
        cat <<EOF
    Usage: $0 [options] [--] [file...]
    
    Arguments:
    
      -h, --help
        Display this usage message and exit.
    
      -f <val>, --foo <val>, --foo=<val>
        Documentation goes here.
    
      -b <val>, --bar <val>, --bar=<val>
        Documentation goes here.
    
      --
        Treat the remaining arguments as file names.  Useful if the first
        file name might begin with '-'.
    
      file...
        Optional list of file names.  If the first file name in the list
        begins with '-', it will be treated as an option unless it comes
        after the '--' option.
    EOF
    }
    
    # handy logging and error handling functions
    log() { printf '%s\n' "$*"; }
    error() { log "ERROR: $*" >&2; }
    fatal() { error "$*"; exit 1; }
    usage_fatal() { error "$*"; usage >&2; exit 1; }
    
    # parse options
    foo="foo default value goes here"
    bar="bar default value goes here"
    while [ "$#" -gt 0 ]; do
        arg=$1
        case $1 in
            # convert "--opt=the value" to --opt "the value".
            # the quotes around the equals sign is to work around a
            # bug in emacs' syntax parsing
            --*'='*) shift; set -- "${arg%%=*}" "${arg#*=}" "$@"; continue;;
            -f|--foo) shift; foo=$1;;
            -b|--bar) shift; bar=$1;;
            -h|--help) usage; exit 0;;
            --) shift; break;;
            -*) usage_fatal "unknown option: '$1'";;
            *) break;; # reached the list of file names
        esac
        shift || usage_fatal "option '${arg}' requires a value"
    done
    # arguments are now the file names
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to read a collection of extension elements with Universal Feed
Is there a way to tell if a feed is XML, JSON, or both?
is there a way to get the link from digg through its rss feed?
Is there a way to create an RSS feed that only customers who have
Is there a way to write standard comments in a Makefile to later feed
Is there a way to parse feed's custom elements? Not feed entries', feed's custom
Is there any way of getting the RSS feed of a LinkedIn group (like
Is there a way to add the thumbnail image url to the wordpress feed??
I have a link that renders an rss feed, is there a way to
Is there a way to search specific type of feed of the user by

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.