help-bash
[Top][All Lists]
Advanced

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

Re: condition to execute


From: Lawrence Velázquez
Subject: Re: condition to execute
Date: Mon, 05 Jul 2021 00:43:40 -0400
User-agent: Cyrus-JMAP/3.5.0-alpha0-530-gd0c265785f-fm-20210616.002-gd0c26578

On Mon, Jul 5, 2021, at 12:03 AM, lisa-asket@perso.be wrote:
> Would the following be good to source my .bashrc when the value
> 
> of the variable f is 1?
> 
> 
> 
>  (( $f == 1 )) && source ${HOME}/.bashrc

You don't have to use '$' to reference variables in arithmetic
contexts.  (I am going to assume, perhaps unwisely, that f takes
on known safe values and doesn't require validation.)  Additionally,
expanding parameters outside of double quotes is very often a bug
(although $HOME is unlikely to cause you problems).  It's easily
addressed by adding double quotes or using ~ instead.

    (( f == 1 )) && source ~/.bashrc

A relatively minor consideration is that this list has a nonzero
exit status if f != 1.  This suggests failure, even if it is not
actually a failure condition.  (It's impossible to judge, given the
utter lack of context.)  Many people would not care about this, and
perhaps you don't either, but if f != 1 is not actually a failure
condition, then using 'if' avoids implying that it is.

    if (( f == 1 )); then source ~/.bashrc; fi

-- 
vq



reply via email to

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