Pass bash script parameters to sub-process unchanged

BashScriptingParameters

Bash Problem Overview


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.

Bash Solutions


Solution 1 - Bash

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.

Solution 2 - Bash

You have to put the $@ in quotes:

/the/exe "$@"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEMPView Question on Stackoverflow
Solution 1 - BashddaaView Answer on Stackoverflow
Solution 2 - BashsthView Answer on Stackoverflow