63

I want to write a simple bash script that will act as a wrapper for an executable. How do I pass all the parameters that script receives to the executable? I tried

/the/exe $@

but this doesn't work with quoted parameters, eg.

./myscript "one big parameter"

runs

/the/exe one big parameter

which is not the same thing.

3 Answers 3

89

When a shell script wraps around an executable, and if you do not want to do anything after the executable completes (that's a common case for wrapper scripts, in my experience), the correct way to call the executable is:

exec /the/exe "$@"

The exec built-in tells the shell to just give control to the executable without forking.

Practically, that prevents a useless shell process from hanging around in the system until the wrapped process terminates.

That also means that no command can be executed after the exec command.

Sign up to request clarification or add additional context in comments.

Comments

20

You have to put the $@ in quotes:

/the/exe "$@"

1 Comment

Great, thank you! I thought that would have put all the parameters in one set of quotes, but it works correctly.
0

Call the program "as such" with exec -

exec /path/to/program

You can use the $@ shell alias pass through all arguments. Note that the arguments may have white-space or special characters, so surround with double-quotes -

exec /path/to/program "$@"

You should also use the -a switch to pass on the program's name, as called. It is common practice to use this argument (argument 0) to look for configuration files with names like .<program-name>.

exec -a $0 /path/to/program "$@"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.