Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers

up vote 11 down vote accepted

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
share|improve this answer
    
Excellent answer, thanks! –  Andrew Oct 10 '09 at 16:56
add comment

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.

share|improve this answer
1  
Slightly wrong - they function the same in both bash and ksh –  MikeyB Oct 10 '09 at 4:10
add comment

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
share|improve this answer
    
Good answer, but slightly wrong. –  MikeyB Oct 10 '09 at 4:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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