Kvass
Kvass

Reputation: 8434

Bash - pass arguments same way they were received

I have a bash alias for my hg (mercurial) command that looks like this:

hg() {
  if [[ $1 == 'up' ]]; then
    if [[ $# == 2 ]]; then
      _update_bookmark $2
    else
      up
    fi
  else
    /bin/hg $@
  fi
}

My issue is with the final else case. I want the arguments, as I received them in hg(), to be passed exactly the same way to /bin/hg. My issue arises when I do something like hg commit -m "my message here" and it gets passed as something like /bin/hg commit -m my message here. I tried both $@ and $* - not sure which is more appropriate here but neither seemed to do the trick. Any suggestions?

Upvotes: 0

Views: 29

Answers (1)

ruakh
ruakh

Reputation: 183300

You need to write "$@" (with double-quotes) to prevent word splitting while still keeping already-separate arguments separate.

Upvotes: 4

Related Questions