2

Suppose I have an executable xyz that takes a variable number of command-line arguments, and a wrapper Korn shell script xyz.ksh. Is there an easy way to pass all of the shell script arguments as-is to the executable?

An̲̳̳drew
  • 1,333

3 Answers3

11

You need to use:

"$@"

for correct parameter expansion in all cases. This behaviour is the same in both bash and ksh.

Most of the time, $* or $@ will give you what you want. However, they expand parameters with spaces in it. "$*" gives you all parameters reduced down to one. "$@" gives you what was actually passed to the wrapper script.

See for yourself (again, under either bash or ksh):

[tla ~]$ touch file1 file2 space\ file
[tla ~]$ ( test() { ls $*; }; test file1 file2 space\ file )
ls: cannot access space: No such file or directory
ls: cannot access file: No such file or directory
file1  file2
[tla ~]$ ( test() { ls $@; }; test file1 file2 space\ file )
ls: cannot access space: No such file or directory
ls: cannot access file: No such file or directory
file1  file2
[tla ~]$ ( test() { ls "$*"; }; test file1 file2 space\ file )
ls: cannot access file1 file2 space file: No such file or directory
[tla ~]$ ( test() { ls "$@"; }; test file1 file2 space\ file )
file1  file2  space file
MikeyB
  • 40,079
1

I think you're looking for the $* variable: http://www.well.ox.ac.uk/~johnb/comp/unix/ksh.html#commandlineargs

It's otherwise known as $@ in bash, I think.

Matt Simmons
  • 20,584
0

Yes. Use the $* variable. Try this script:

#!/bin/ksh

echo $*

and then invoke the script with something like:

scriptname a b c foobar

Your output will be:

a b c foobar
user10501
  • 682