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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:32:13+00:00 2026-06-17T14:32:13+00:00

I am trying to make a shell script which is designed to be run

  • 0

I am trying to make a shell script which is designed to be run like this:

script.sh -t application

Firstly, in my script I want to check to see if the script has been run with the -t flag. For example if it has been run without the flag like this I want it to error:

script.sh

Secondly, assuming there is a -t flag, I want to grab the value and store it in a variable that I can use in my script for example like this:

FLAG="application"

So far the only progress I’ve been able to make on any of this is that $@ grabs all the command line arguments but I don’t know how this relates to flags, or if this is even possible.

  • 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-17T14:32:14+00:00Added an answer on June 17, 2026 at 2:32 pm

    You should read this getopts tutorial.

    Example with -a switch that requires an argument :

    #!/bin/bash
    
    while getopts ":a:" opt; do
      case $opt in
        a)
          echo "-a was triggered, Parameter: $OPTARG" >&2
          ;;
        \?)
          echo "Invalid option: -$OPTARG" >&2
          exit 1
          ;;
        :)
          echo "Option -$OPTARG requires an argument." >&2
          exit 1
          ;;
      esac
    done
    

    Like greybot said(getopt != getopts) :

    The external command getopt(1) is never safe to use, unless you know
    it is GNU getopt, you call it in a GNU-specific way, and you ensure
    that GETOPT_COMPATIBLE is not in the environment. Use getopts (shell
    builtin) instead, or simply loop over the positional parameters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a shell script to remove special characters, like {}()!,'
I'm currently trying to make some small shell-like utility for a custom script I
I am trying to make a new command which runs a shell script that
I'm trying to modify this very simple shell script I wrote to check if
I am trying to make an interactive shell script in Perl. The only user
Trying to make a simple number clicker control for BlackBerry 6/7, like this: At
I'm trying to write a shell script that will make several targets into several
I'm trying to write a shell script which recursively goes through a directory, then
I am trying to optimize my shell script a bit and want to be
I am trying to do this: I want to call a make ( Makefile

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.