helpThe GNU Bourne-Again SHell - Support: sr #108991, Nested conditional command...


sr #108991: Nested conditional command evaluates outside function

Submitted by:  Troy Etherton <troy>
Submitted on:  Fri 04 Mar 2016 03:36:27 PM UTC  
Category: NonePriority: 5 - Normal
Severity: 2 - MinorStatus: Invalid
Privacy: PublicAssigned to: None
Open/Closed: OpenOperating System: GNU/Linux

Add a New Comment(Rich Markup)

You are not logged in

Please log in, so followups can be emailed to you.


Thu 10 Mar 2016 01:32:00 AM UTC, comment #1:

This is not a bug. Let's break it down.

The && and || operators are left-associative. That means that

[[ foo ]] && f1 || f2

is parsed as

{ [[ foo ]] && f1; } || f2

so the exit status of the first command (the stuff before the `||') determines whether or not f2 is executed.

When you pass "Function1" to the script as $1, the conditional command succeeds, and Function1 is executed. The return status of Function1 is the return status of DEBUG, the last command executed in Function1's body. The return status of DEBUG is false, because the status of the conditional command is false. Since DEBUG returns failure, Function1 fails, the whole command to the left of the `||' fails, and Function2 is executed.

This is one of the ways c1 && c2 || c3 is not equivalent to an if statement.

Chet Ramey <chet>
Project Administrator
Fri 04 Mar 2016 03:36:27 PM UTC, original submission:

Not sure if this is a bug or my own misunderstanding of how conditional commands should work within BASH.

At the end of the script, I use a conditional command to choose between two functions using an OR statement. If I include a conditional command (test for _DEBUG == on) inside of, and at the end of the first function (Function1), and that condition results in a FALSE, the second function after the OR in my original conditional command is executed.

If the nested conditional command is moved before another valid command, such as before the echo statement in Function1, the problem does not occur. The nested conditional command seems to pass on its OR to the original conditional command.

Example script. Pass 'Function1' as a param to see it fail.
# ==================== BEGIN SCRIPT ====================

function DEBUG()
[ "$_DEBUG" == "on" ] && echo -e "\e[1;34m$@\e[m"

echo "Hello, I'm function one"
DEBUG Leaving Function1

echo "Hello, I'm function two"

[[ "$1" = "Function1" ]] && Function1 || Function2
# ====================== END SCRIPT ======================

Troy Etherton <troy>


(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach File(s):

No files currently attached


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -unavailable- added by chet (Posted a comment)
  • -unavailable- added by troy (Submitted the item)
  • -unavailable- added by troy

    Do you think this task is very important?
    If so, you can click here to add your encouragement to it.
    This task has 0 encouragements so far.

    Only logged-in users can vote.


    Please enter the title of George Orwell's famous dystopian book (it's a date):



    Follow 2 latest changes.

    Date Changed By Updated Field Previous Value => Replaced By
    Thu 10 Mar 2016 01:32:00 AM UTCchetStatusNone=>Invalid
    Fri 04 Mar 2016 03:36:27 PM UTCtroyCarbon-Copy-=>Added -unavailable-

    Back to the top

    Powered by Savane 3.1-cleanup1