Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: How to escape $@?

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.

like image 223
Alexander Gladysh Avatar asked Dec 31 '10 09:12

Alexander Gladysh


1 Answers

Quote $@, making it "$@":

$ cat >proxy.sh 
#!/bin/bash

./script.sh "$@"
^D

Then it retains the original quotes:

one a b
two c
three
like image 77
moinudin Avatar answered Oct 06 '22 04:10

moinudin