Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[ $1 != "frps" -a $1 != "frpc" ] vs [[ $1 != "frps" ]] && [[ $1 != "frpc" ]]

7 views
Skip to first unread message

hongy...@gmail.com

unread,
Nov 4, 2021, 1:34:00 AM11/4/21
to
In bash script, both of the following usages are valid:

[ $1 != "frps" -a $1 != "frpc" ]
[[ $1 != "frps" ]] && [[ $1 != "frpc" ]]

Which is preferable?

Regards,
HZ


Josef Moellers

unread,
Nov 4, 2021, 4:13:16 AM11/4/21
to
The one which is most easily understandable.

In my (40+ years) experience this beats just about EVERTHING ELSE, apart
from maybe EXTREME performance differences and/or local conventions.
If you look at the code 3 months from now (or maybe even longer, say
tomorrow) and STILL understand it, it is fine.

Josef

Janis Papanagnou

unread,
Nov 4, 2021, 5:40:14 AM11/4/21
to
On 04.11.2021 09:13, Josef Moellers wrote:
> On 04.11.21 06:33, hongy...@gmail.com wrote:
>> In bash script, both of the following usages are valid:
>>
>> [ $1 != "frps" -a $1 != "frpc" ]
>> [[ $1 != "frps" ]] && [[ $1 != "frpc" ]]
>>
>> Which is preferable?

Depending on the actual requirements (portability anyone?), probably
none of these.

For the first case; POSIX: ">4 arguments: The results are unspecified."
(BTW: Why did you quote the constant literals but not the variables?)

If you don't care about portability and standards use [[...]] .

Many folks forget that we can formulate (even regexp-)comparisons also
with the [standard] 'case' construct

case $1 in (frps|frpc) ;; (*) ... ; esac

>
> The one which is most easily understandable.

...and which shows the least surprises (generally and in your context).

Janis

hongy...@gmail.com

unread,
Nov 4, 2021, 7:21:28 AM11/4/21
to
On Thursday, November 4, 2021 at 5:40:14 PM UTC+8, Janis Papanagnou wrote:
> On 04.11.2021 09:13, Josef Moellers wrote:
> > On 04.11.21 06:33, hongy...@gmail.com wrote:
> >> In bash script, both of the following usages are valid:
> >>
> >> [ $1 != "frps" -a $1 != "frpc" ]
> >> [[ $1 != "frps" ]] && [[ $1 != "frpc" ]]
> >>
> >> Which is preferable?
> Depending on the actual requirements (portability anyone?), probably
> none of these.
>
> For the first case; POSIX: ">4 arguments: The results are unspecified."
> (BTW: Why did you quote the constant literals but not the variables?)

Negligence or bad habit of mine :-(

> If you don't care about portability and standards use [[...]] .
>
> Many folks forget that we can formulate (even regexp-)comparisons also
> with the [standard] 'case' construct
>
> case $1 in (frps|frpc) ;; (*) ... ; esac

Which type of regexp is supported here?

Janis Papanagnou

unread,
Nov 4, 2021, 7:30:03 AM11/4/21
to
On 04.11.2021 12:21, hongy...@gmail.com wrote:
> On Thursday, November 4, 2021 at 5:40:14 PM UTC+8, Janis Papanagnou wrote:
>> On 04.11.2021 09:13, Josef Moellers wrote:
>>> On 04.11.21 06:33, hongy...@gmail.com wrote:
>>>> In bash script, both of the following usages are valid:
>>>>
>>>> [ $1 != "frps" -a $1 != "frpc" ]
>>>> [[ $1 != "frps" ]] && [[ $1 != "frpc" ]]
>>>>
>>>> Which is preferable?
>> Depending on the actual requirements (portability anyone?), probably
>> none of these.
>>
>> For the first case; POSIX: ">4 arguments: The results are unspecified."
>> (BTW: Why did you quote the constant literals but not the variables?)
>
> Negligence or bad habit of mine :-(
>
>> If you don't care about portability and standards use [[...]] .
>>
>> Many folks forget that we can formulate (even regexp-)comparisons also
>> with the [standard] 'case' construct
>>
>> case $1 in (frps|frpc) ;; (*) ... ; esac
>
> Which type of regexp is supported here?

Sorry, it's techncally not "regexps" (BRE, ERE) but Shell Patterns.
(The same that are also supported in the non-standard 'if [[ ... ]]'
on the right hand side of the comparison operator.)

Janis
0 new messages