Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This very well may be stupidity on my part. I would like to accept Yes/No, True/False responses within my Bash script:




echo User passed in $ARGUMENT


if [[ "$OPTARG" -eq 0 ]] || [[ "${OPTARG^^}" == "N" ]]; then
    echo Decline
elif [[ "$OPTARG" -eq 1 ]] || [[ "${OPTARG^^}" == "Y" ]]; then
    echo Accept
    echo Invalid argument

Here are some sample results:

./test.sh 0

User passed in 0

./test.sh 1

User passed in 1

./test.sh 2

User passed in 2
Invalid argument

./test.sh No

User passed in No

Now the stumper. Why is [[ "${OPTARG^^}" == "N" ]] returning true for any string!?

./test.sh Yes

User passed in Yes

./test.sh ThisShouldNotMatch

User passed in ThisShouldNotMatch
share|improve this question
what happens if you quote this? OPTARG="${ARGUMENT:0:1}" – mikeserv Mar 15 '14 at 16:20
Nevermind. Goldilocks is right. – mikeserv Mar 15 '14 at 16:20
up vote 10 down vote accepted


if [[ "$OPTARG" -eq 0 ]] 

Will match any string, unless it is just digits. Use:

if [[ "$OPTARG" == "0" ]] 


share|improve this answer

Although the problem in your code has been well explained by goldilocks, I cannot resist saying that your code is needlessly long. You could simplify it a lot.

The following is equivalent to your desired code:

echo User passed in $1
if [[ "$1" == [nN0]* ]]; then      # First argument starts with either n, N, or 0
    echo Decline
elif [[ "$1" == [yY1]* ]]; then    # First argument starts with either y, Y, or 1
    echo Accept
    echo Invalid argument

You might also want to refer to Conditional Constructs in the manual.

share|improve this answer
Thank you, devnull. I will definitely use this trick in future scripts! I was torn as to whether I should accept your answer, but I think the distinctions in goldilocks' answer may be helpful for others. – i_grok Mar 16 '14 at 14:23
@i_grok I did mention in my answer that goldilocks' answer points to the problem. I simply presented an alternate approach that seemed more readable. It's good to see multiple approaches to solving a problem come up. Regarding which answer is accepted is a relatively trivial affair. – devnull Mar 16 '14 at 14:39

Also you can use case

read -p 'Do you want to Continue (yes/no?): ' 
case $REPLY in
       [yY]|[yY][eE][sS]) echo Accept             ;;
           [nN]|[nN][oO]) echo Decline            ;;
                       *) echo "Invalid argument" ;;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.