help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: the fine art of error chek'n


From: Alan D. Salewski
Subject: Re: the fine art of error chek'n
Date: Thu, 7 May 2020 11:47:18 -0400
User-agent: Mutt/1.13.2 (2019-12-18)

On 2020-05-07 15:39:05, address@hidden spake thus:
> i have written simple programs use'n bash for many years
> i most always use something like
> 
> false
> if (($? != 0)); then echo failed; fi
> 
> other than readability what is the difference in the above and
> 
> false || echo failed

The arithmetic expression is less portable to other Bourne-family shells. For
example, here is what dash does with it:

    $ false || echo failed
    failed

    $ false
    $ if (($? != 0)); then echo failed; fi
    /bin/dash: 10: 1: not found

A portable way to do the same check would be:

    $ false
    $ if test $? -ne 0; then echo failed; fi
    failed

The shell portability chapter of the GNU Autoconf manual has a section that
discusses the different ways parentheses are treated by different shells:

    11.10 "Parentheses in Shell Scripts"
    
https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/autoconf.html#Parentheses

Take care,
-Al

-- 
-----------------------------------------------------------------
a l a n   d.   s a l e w s k i                   address@hidden
1024D/FA2C3588 EDFA 195F EDF1 0933 1002  6396 7C92 5CB3 FA2C 3588
-----------------------------------------------------------------


reply via email to

[Prev in Thread] Current Thread [Next in Thread]