3

I have a bash script in which I want to check if a flag with value was passed to it and then use the value in a variable. Something like this (pseudocode):

file.sh -c 1.0.0 

inside file.sh :

#!/bin/bash get flag: if flag 'c' then curl c else curl 'something else' 

Whats the most optimal way to do the above?

3
  • @TharangaAbeyseela what if no argument if passed ? the script would just fail. Commented Feb 17, 2017 at 22:01
  • please check the answer. sorry i hit the enter before completion :) Commented Feb 17, 2017 at 22:09
  • BashFAQ #35 covers the topic in detail. Note that getopt is not a preferred solution. Commented Feb 17, 2017 at 22:10

1 Answer 1

3

try the following

#!/bin/bash while getopts ":c" opt; do case $opt in c) echo "-c was triggered!" >&2 ;; \?) echo "Invalid option: -$OPTARG" >&2 ;; esac done 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.