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?
Asked
Active
Viewed 3,928 times
3 Answers
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
- 39,291
- 10
- 105
- 189
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,396
- 10
- 68
- 116
-
1Slightly wrong - they function the same in both bash and ksh – MikeyB Oct 10 '09 at 04: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

user10501
- 682
- 1
- 5
- 7