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

Re: Creating function in zsh



On Thu, Oct 16, 2008 at 12:59:39PM -0700, czech@xxxxxxxxx wrote:
> Hi -
> 
> I have created the following function in zsh on Mac OSX:
> 
> cd() {
>     builtin cd "$@" print -D $PWD
> }
> 
> This works find on OSX, but gives the following error on Fedora Core 8:
> 
> johann:czechar:.zfunc> cd /home/czechar
> cd /home/czechar
> cd:cd:1: too many arguments
> 
> I am running zsh 4.2.6 on Fedora.
> 
> Any notion what the problem could be?
[...]

I doubt it works on MaxOSX. You're not separating the two
commands, so "print", "-D" and $PWD are treated as arguments to
"builtin cd".

cd() {
  builtin cd "$@" &&
    print -rD $PWD
}

"&&" as you probably want to print the current directory only if
cd succeeds.

You need -r, as you don't want \x sequences to be expanded.

If you want to print the directory regardless of whether cd
succeeds, you should at least make sure you return cd's exit
status:

cd() {
  local ret
  builtin cd "$@"
  ret=$?
  print -rD $PWD
  return $ret
}

-- 
Stéphane



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