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?

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
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.

  • 1
    Slightly wrong - they function the same in both bash and ksh – MikeyB Oct 10 '09 at 4:10
  • @MikeyB Wrong, at least for bash. $* passes all parameters as a single string, wheres $@ passes all parameter as a list of separate strings. From the bash manual: "$*" is equivalent to "$1c$2c..." and ` $@ is equivalent to "$1" "$2" .... There are cases where you need need to use $* whereas $@ will not work. For bash related stuff use man bash and search for the terms you need. – sjas Feb 20 '15 at 19:45
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
  • Good answer, but slightly wrong. – MikeyB Oct 10 '09 at 4:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.