Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash - pass script as argument of another script

Tags:

bash

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.

like image 585
buydadip Avatar asked Jan 04 '15 20:01

buydadip


People also ask

How do I pass arguments from one bash script to another?

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.

Can you pass arguments to a bash 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.


1 Answers

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
like image 70
Cameron Martin Avatar answered Oct 07 '22 06:10

Cameron Martin