-1

I'm trying to replicate Bash's help command in my .zshrc. I thought this would work:

function help {
  bash -c "help $@"
}

but this only works when I only pass a single argument -- for instance, help exit. It does not work when I pass options to help, such as help -m exit (which works in Bash). However, literally typing bash -c "help -m exit" works as expected. I imagine this has something to do with how quoting works in the shell, but I can't figure this out.

BallpointBen
  • 9,406
  • 1
  • 32
  • 62

2 Answers2

2

You can use

function help {
  bash -c "help $*"
}
ehwas
  • 248
  • 1
  • 9
  • Thanks, that fixed it. I understand why `$*` works, but I don't really understand why `$@` doesn't. I could see how in some contexts, such as looping over a list, `$@` and `$*` would be different, but could you explain how they function differently for just dropping the variables into another string? – BallpointBen Dec 11 '16 at 05:51
  • 1
    $* returns all the passed arguments in one quoted string $@ returns them as separate quoted strings and in your case are passed as multiple arguments to bash - the final call seems something like bash -c "help funcarg1" "funcarg2" "funcargn" Probably bash ignores the other parameters. You can read more here: http://tldp.org/LDP/abs/html/internalvariables.html#APPREF – ehwas Dec 11 '16 at 05:56
  • `bash` doesn't ignore the other arguments; `$0` is set to `funcarg2` and `$1` is set to `funcargn`. The command you specified to run, `help funcarg1`, does ignore the values of `$0` and `$1`, though. – chepner Dec 11 '16 at 16:38
0

I suppose you need help for using zsh, not for bash. Bash doesn't know anything about zsh builtins and features, and the common builtins are different enough to warrant separate documentation pages.

The equivalent of bash's help builtin is run-help, but it is not active by default. It will also call man for you and comes with a few useful wrappers.

TLDR; put this in .zshrc

autoload -Uz run-help autoload -Uz run-help-git autoload -Uz run-help-ip autoload -Uz run-help-openssl autoload -Uz run-help-p4 autoload -Uz run-help-sudo autoload -Uz run-help-svk autoload -Uz run-help-svn alias help=run-help

https://wiki.archlinux.org/index.php/zsh#Help_command

Marco Mariani
  • 13,556
  • 6
  • 39
  • 55