Correct way to check for a command line flag in bash
Asked Answered
H

11

72

In the middle of a script, I want to check if a given flag was passed on the command line. The following does what I want but seems ugly:

if echo $* | grep -e "--flag" -q
then
  echo ">>>> Running with flag"
else
  echo ">>>> Running without flag"
fi

Is there a better way?

Note: I explicitly don't want to list all the flags in a switch/getopt. (In this case any such things would become half or more of the full script. Also the bodies of the if just set a set of vars)

Hyams answered 20/5, 2010 at 15:45 Comment(0)
I
91

An alternative to what you're doing:

if [[ $* == *--flag* ]]

See also BashFAQ/035.

Note: This will also match --flags-off since it's a simple substring check.

Ideational answered 20/5, 2010 at 17:16 Comment(14)
Won't that also pick up false positives? e.g. if you chech for *-f* it will match --force, -fish, --update-files, -w-t-fetcHeth
@nathanchere: careful choice of match string is important. Note that your example allows for all the matches that you list but mine leaves much fewer possibilities of false positives. Given the OP's requirement for a simple solution mine fits quite well. Otherwise use getopt or getopts.Ideational
Exactly my point. Mine was an intentionally simplified example, but still demonstrates the same issue with yours. e.g. if you use *--flag* you will match --flag, --flags-off etc. i.e. it's a sloppy solutionHeth
@Heth You're welcome to post a better solution that meets the OP's requirements.Ideational
Could you please tell how to get the ordinal of that $*?Dillondillow
@OptimusPrime: Why do you want the ordinal?Ideational
Let's say the flag is at 5th position, then $6 would give me its value.Dillondillow
@OptimusPrime: The OP wanted a quick and dirty way to check for a flag without using getopt. For complete processing of flags with (and/or without) arguments, you should use techniques as described in the FAQ I linked to.Ideational
Is there no need for "$*"?Coralloid
@Randoms: If you're asking whether there's a need for quoting - it's not necessary inside double square brackets.Ideational
My small modification that solves the mentioned problem of the false positivesFm
This does not work in Bash at all...Saltcellar
@Cerin: Yes it does, but "does not work" conveys no information at all. What did you try and what was the result?Ideational
@DennisWilliamson My mistake. Your solution only works in the top-level scope, where I was trying to use it inside a function, which of course redefines $*. Storing the top level arguments to a variable made it work for my case.Saltcellar
W
21

I typically see this done with a case statement. Here's an excerpt from the git-repack script:

while test $# != 0
do
    case "$1" in
    -n) no_update_info=t ;;
    -a) all_into_one=t ;;
    -A) all_into_one=t
        unpack_unreachable=--unpack-unreachable ;;
    -d) remove_redundant=t ;;
    -q) GIT_QUIET=t ;;
    -f) no_reuse=--no-reuse-object ;;
    -l) local=--local ;;
    --max-pack-size|--window|--window-memory|--depth)
        extra="$extra $1=$2"; shift ;;
    --) shift; break;;
    *)  usage ;;
    esac
    shift
done

Note that this allows you to check for both short and long flags. Other options are built up using the extra variable in this case.

Worsham answered 20/5, 2010 at 15:53 Comment(2)
I think you have to remove the last 'shift' statement, right after the 'esac'. At least on my machine it doesn't work if it's present.Memling
@ThomasKrille The shift at the end is necessary to move onto the next argument. If you remove it, it would run infinitely (unless you're using one of the options that have shift in the case).Mcarthur
G
16

you can take the straight-forward approach, and iterate over the arguments to test each of them for equality with a given parameter (e.g. -t, --therizinosaurus).

put it into a function:

has_param() {
    local term="$1"
    shift
    for arg; do
        if [[ $arg == "$term" ]]; then
            return 0
        fi
    done
    return 1
}

… and use it as a predicate in test expressions:

if has_param '-t' "$@"; then
    echo "yay!"
fi

if ! has_param '-t' "$1" "$2" "$wat"; then
    echo "nay..."
fi

if you want to reject empty arguments, add an exit point at the top of the loop body:

for arg; do
    if [[ -z "$arg" ]]; then
        return 2
    fi
    # ...

this is very readable, and will not give you false positives, like pattern matching or regex matching will.
it will also allow placing flags at arbitrary positions, for example, you can put -h at the end of the command line (not going into whether it's good or bad).


but, the more i thought about it, the more something bothered me.

with a function, you can take any implementation (e.g. getopts), and reuse it. encapsulation rulez!
but even with commands, this strength can become a flaw. if you'll be using it again and again, you'll be parsing all the arguments each time.

my tendency is to favor reuse, but i have to be aware of the implications. the opposed approach would be to parse these arguments once at the script top, as you dreaded, and avoid the repeated parsing.
you can still encapsulate that switch case, which can be as big as you decide (you don't have to list all the options).

Girgenti answered 3/6, 2019 at 16:24 Comment(2)
This was the best answer!Madge
great answer. Note this can also be used for multiple parameters. i found this cleaner then getopts as well.Chifforobe
R
8

You can use the getopt keyword in bash.

From http://aplawrence.com/Unix/getopts.html:

getopt

This is a standalone executable that has been around a long time. Older versions lack the ability to handle quoted arguments (foo a "this won't work" c) and the versions that can, do so clumsily. If you are running a recent Linux version, your "getopt" can do that; SCO OSR5, Mac OS X 10.2.6 and FreeBSD 4.4 has an older version that does not.

The simple use of "getopt" is shown in this mini-script:

#!/bin/bash
echo "Before getopt"
for i
do
  echo $i
done
args=`getopt abc:d $*`
set -- $args
echo "After getopt"
for i
do
  echo "-->$i"
done
Rowlett answered 20/5, 2010 at 15:50 Comment(4)
It's better to use the builtin getopts rather than the external getopt.Ideational
@Dennis: getopts supports long option names like --flag?Upcast
@indiv: Oh, sorry, I overlooked that requirement. I would use a case statement before I would use getopt. See this for a comparison of getopt and getopts.Ideational
From the faq in the above answer: getopts: Unless it's the version from util-linux, and you use its advanced mode, never use getopt(1). Traditional versions of getopt cannot handle empty argument strings, or arguments with embedded whitespace.Banausic
D
4

I've made small changes to the answer of Eliran Malka:

This function can evaluate different parameter synonyms, like "-q" and "--quick". Also, it does not use return 0/1 but an echo to return a non-null value when the parameter is found:

function has_param() {
    local terms="$1"
    shift

    for term in $terms; do
        for arg; do
            if [[ $arg == "$term" ]]; then
                echo "yes"
            fi
        done
    done
}

# Same usage:

# Assign result to a variable.
FLAG_QUICK=$(has_param "-q --quick" "$@")  # "yes" or ""

# Test in a condition using the nonzero-length-test to detect "yes" response.
if [[ -n $(has_param "-h --help" "$@") ]]; then; 
    echo "Need help?"
fi

# Check, is a flag is NOT set by using the zero-length test.
if [[ -z $(has_param "-f --flag" "$@") ]]; then
    echo "FLAG NOT SET"
fi
Dinadinah answered 16/9, 2020 at 12:0 Comment(3)
nice! piping the data back as output is an awesome addition - it streamlines the command api greatly, and adding the ability to check a list of terms is also quite cool (though the external loop will further strain on performance..). <3Girgenti
meta - sometimes stackoverflow is a bit like github. should we add that SO fork feature already and be done with it? :DGirgenti
Note, if your doing the check inside a function, you can't use "$@", because that will refer to the function's arguments. You'd need to cache the script's arguments in a global variable.Saltcellar
F
4

The modification of Dennis Williamson's answer with additional example for a argument in the short form.

if [[ \ $*\  == *\ --flag\ * ]] || [[ \ $*\  == *\ -f\ * ]]

It solves the problem of false positive matching --flags-off and even --another--flag (more popular such case for an one-dashed arguments: --one-more-flag for *-f*).

\ (backslash + space) means space for expressions inside [[ ]]. Putting spaces around $* allows to be sure that the arguments contacts neither line's start nor line's end, they contacts only spaces. And now the target flag surrounded by spaces can be searched in the line with arguments.

Fm answered 20/1, 2021 at 14:50 Comment(0)
L
4
if [ "$1" == "-n" ]; then
    echo "Flag set";
fi
Lanthanide answered 24/2, 2021 at 20:32 Comment(2)
this will only work if the flag was the first argument passed in the command.Berar
And this will fail if the flag is not set and you have set -u, which you wantFellows
B
3

Here is a variation on the most voted answer that won't pick up false positives

if [[ " $* " == *" -r "* ]]; then
Berar answered 2/1, 2023 at 16:23 Comment(0)
S
1

One more variation:

flag1="false"
if [[ " $@ " =~ " --flag1 " ]]; then
    flag1="true"
fi

flag2="false"
if [[ " $@ " =~ " --flag2 " ]]; then
    flag2="true"
fi
Selfdeception answered 27/3, 2023 at 23:20 Comment(0)
P
0

Mb something like:

if [[ ($* == -f) || ($* == --flag) ]];
then
  echo ">>>> Running with flag"
else
  echo ">>>> Running without flag"
fi
Paragon answered 18/5, 2023 at 19:48 Comment(0)
B
-1

Not an alternative, but an improvement, though.

if echo $* | grep -e "\b--flag\b" -q

Looking for word boundaries will make sure to really get the option --flag and neither --flagstaff nor --not-really--flag

Bartholomeus answered 19/11, 2020 at 14:2 Comment(1)
This will not work. E.g. echo '--foo-bar' | grep -e "--foo\b" fails. See explanation of why hereSunil

© 2022 - 2024 — McMap. All rights reserved.