I can't find a similar question on SO.
How can I properly pass a bash script as an argument to another bash script.
For example, let's say I have two scripts that can each accept a number of parameters, I want to pass one script as the argument of the other. Something like:
./script1 (./script2 file1 file2) file3
In the above example, script2 merges file1 and file2 together, and echos a new file, however that is irrelevant to the question. I just want to know how I can pass script2
as a parameter, i.e. the proper syntax.
If this is not possible, any hint as to how I may circumvent the issue would be appropriate.
Arguments can be passed to the script when it is executed, by writing them as a space-delimited list following the script file name. Inside the script, the $1 variable references the first argument in the command line, $2 the second argument and so forth. The variable $0 references to the current script.
Passing arguments before runningWe can pass parameters just after the name of the script while running the bash interpreter command. You can pass parameters or arguments to the file.
If you want to pass the result of evaluating script2 as a parameter, use $()
. Remember that you have to quote it.
./script1 "$(./script2 file1 file2)" file3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With