what does || mean when declaring variables


Hi guys im wondering how does || work when declaring variables? You can see this in the 3rd line in the code below. $output is set to a function and then the $error variable is set to the exit code of the previous command after the ||. What does || do in this situation/how is it handled?

 if [ "$ENABLED" = "yes" ] || [ "$ENABLED" = "YES" ]; then
    log_action_begin_msg "Starting firewall:" "ufw"
    output=`ufw_start` || error="$?"  <-- HERE
    if [ "$error" = "0" ]; then
        log_action_cont_msg "Setting kernel variables ($IPT_SYSCTL)"
    if [ ! -z "$output" ]; then
        echo "$output" | while read line ; do
            log_action_cont_msg "$line"
    log_action_begin_msg "Skip starting firewall:" "ufw (not enabled)"


In general, the exit status of an assignment is 0. But when a command substitution is present, the exit status is the exit status of the command substitution, in this case of ufw_start.

So if ufw_start fails, its non-zero exit status is stored in the variable error.

Also, since error is only used to see if its value is 0 or not, it could be eliminated altogether.

if output=$(ufw_start); then
    log_action_cont_msg "..."

Answered By – chepner

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published