Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass bash script parameters to sub-process unchanged

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.

like image 855
EMP Avatar asked Nov 08 '09 09:11

EMP


People also ask

What is $() in bash script?

$() Command Substitution According to the official GNU Bash Reference manual: “Command substitution allows the output of a command to replace the command itself.

Can you pass in an argument to a bash script?

You can pass the arguments to any bash script when it is executed. There are several simple and useful ways to pass arguments in a bash script. In this article guide, we will let you know about some very easy ways to pass and use arguments in your bash scripts.

How do you abort in bash?

This can actually be done with a single line using the set builtin command with the -e option. Putting this at the top of a bash script will cause the script to exit if any commands return a non-zero exit code.


1 Answers

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.

like image 196
ddaa Avatar answered Oct 18 '22 00:10

ddaa