1

I'm trying to use all arguments from my function like this

function docker_run {
  docker run \
    -it --rm \
    --entrypoint "" \
    --volume "$(pwd):/${repo}" \
    --workdir "/${repo}" \
    alpine/git:v2.26.2 \
    "$*"
}

docker_run sh -c "git reset && git add --a && git status"

this gives the error:

docker: Error response from daemon: OCI runtime create failed: container_linux.go:346: starting container process caused "exec: "sh -c git reset && git add --a && git status": executable file not found in $PATH": unknown.

if I remove quotes around $* the error is:

usage: git [--version] [--help] [-C ]...

is *$ the correct variable to use? ideally I'd want docker_run to work with any possible argument combination

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
cooldude101
  • 1,215
  • 2
  • 14
  • 29
  • `"$*"` concatenates the separate arguments into a single space-separated string. Use `"$@"` to preserve the word boundaries. – John Kugelman Jul 29 '20 at 12:20

1 Answers1

1

If you want to use all arguments, you should use $@ and not $*. $@ is special in that it's array-like, so when you put it in quotes it expands to all arguments. $* is a string that joins the arguments with spaces, so it globs into one argument.

Aplet123
  • 33,825
  • 1
  • 29
  • 55