8

I need to write a bash script that, among other things, should pass all its arguments intact to another program.

Minimal example:

 $ cat >proxy.sh #!/bin/bash ./script.sh $@ ^D $ chmod +x proxy.sh $ cat >script.sh #!/bin/bash echo one $1 echo two $2 echo three $3 ^D $ chmod +x script.sh 

This naïve approach does not work for arguments with spaces:

 $ ./proxy.sh "a b" c one a two b three c 

Expected:

 $ ./proxy.sh "a b" c one a b two c three 

What should I write in proxy.sh for this to happen?

Note that I can't use aliases, proxy.sh must be a script — it does some stuff before invoking script.sh.

2
  • 1
    "...should all its arguments intact to another program." -- is that a typo? What did you mean to say? Commented Dec 31, 2010 at 9:17
  • "...should pass all its arguments..." Fixed, sorry. Commented Dec 31, 2010 at 9:19

1 Answer 1

13

Quote $@, making it "$@":

$ cat >proxy.sh #!/bin/bash ./script.sh "$@" ^D 

Then it retains the original quotes:

one a b two c three 
Sign up to request clarification or add additional context in comments.

4 Comments

It works, thanks. I did not expect that, I thought that this would glue all arguments to one... Need to read up on Bash syntax, I guess.
This is correct. $@ must always be quoted, or else it doesn't do what you want (it does the same as $* instead, which is usually a bug).
You also have to enclose all other variables in "double quotes" if you want to keep the spaces in them. Except in a few rare cases, so always write "$var" instead of $var.
does ssh -t some-host -C "some-command $@" quote $@ as desired?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.