help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Why bash does not recoganize array type smartly?


From: Stephane Chazelas
Subject: Re: [Help-bash] Why bash does not recoganize array type smartly?
Date: Thu, 17 Mar 2016 11:33:56 +0000
User-agent: Mutt/1.5.21 (2010-09-15)

2016-03-16 18:40:32 +0000, Dan Douglas:
[...]
> That's basically what my current solution is. I heard zsh has a
> pre-command variable that would be much better but I still need some
> ugly hacks to make it work.
[...]

It's got some hook functions: precmd (PROMPT_COMMAND in bash),
chpwd (could be implemented by wrapping cd/pushd/popd in bash),
periodic herited from tcsh and a few others including preexec
that was badly missing in bash
(see https://github.com/rcaloras/bash-preexec for instance for a
work around), though it looks like PS0 will address that in 4.4.

In zsh, they are functions, which makes sense since it's code,
but note that that means if you want to *add* code to those
hooks, you had to treat them as strings again as with the special
"functions" hash:

functions[precmd]+='
  echo and do that as well'

Or, to be more reliable if you don't have control over what
precmd was before (which might be calling "return" for instance):

functions[precmd]="(){ $functions[precmd]; } "'"$@" || :
 echo and do that as well'

quite ugly.

Now, instead there's a <hook>_functions special array (like
precmd_functions) to which you can add the functions to be run
at each hook and a add-zsh-hook helper function to help you
manage those lists.

In any case, for precmd and preexec, that should be functionaly
equivalent to bash's PROMPT_COMMAND and PS0, as you can define
your PROMPT_COMMAND as:

   PROMPT_COMMAND='
     for fn in "address@hidden";do
       "$fn"
     done'

-- 
Stephane




reply via email to

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