Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: alias with a parameter



> On 07 April 2021 at 13:48 lb@xxxxxxxxxx wrote:
> 
> On 07 Apr 2021, at 01:05, Stephane Chazelas <stephane@xxxxxxxxxxxx> wrote:
> > 2021-04-05 20:47:47 -0600, Grant Taylor:
> > [...]
> >>   pd() {
> >>      [ -n "${1}" ] && pushd "${1}" || popd;
> >>   }
> 
> > That function would run popd when pushd fails. It's generally a
> > bad idea to use this kind of a && b || c in place of proper
> > if/then/else constructs.
> 
> Bad idea, or just a style "this is proper practice" sort of thing?
> 
> I can't imagine any 'bad idea' from this, as it is doing the same basic thing.

Stephane is pointing out that what you want is:

if [[ -n $1 ]]; then
  pushd "$1"
else
  popd
fi

but what you've got is (writing out in full for clarity):

if [[ -n $1 ]]; then
  if ! pushd "$1"; then
    popd
  fi
else
  popd
fi

because of the slightly obscure but nonetheless well-defined way && and || work.

pws




Messages sorted by: Reverse Date, Date, Thread, Author