Bash: How to escape $@?
I need to write a bash script that, among other things, should pass all its arguments intact to another program.
M开发者_开发百科inimal 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
.
Quote $@
, making it "$@"
:
$ cat >proxy.sh
#!/bin/bash
./script.sh "$@"
^D
Then it retains the original quotes:
one a b
two c
three
精彩评论