Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash - How to pass arguments to a script that is read via standard input

Tags:

bash

I am trying execute a script from standard input and also pass arguments to it. Is there a way to do it?

Let's say that I have the following:

cat script.sh | bash 

How would I pass the arguments to the script?

I do not want to do this:

bash script.sh arguments 

Nor this:

./script.sh arguments 
like image 599
dabest1 Avatar asked Dec 15 '11 02:12

dabest1


People also ask

How do you pass input arguments in shell script?

Using arguments Inside the script, we can use the $ symbol followed by the integer to access the arguments passed. For example, $1 , $2 , and so on. The $0 will contain the script name.

How do you pass an argument to a function in bash?

To pass any number of arguments to the bash function simply put them right after the function's name, separated by a space. It is a good practice to double-quote the arguments to avoid the misparsing of an argument with spaces in it. The passed parameters are $1 , $2 , $3 …

What is standard input in bash?

Bash Redirection STDIN, STDOUT and STDERR explained Standard input is used to provide input to a program. (Here we're using the read builtin to read a line from STDIN.) STDOUT root@server~# ls file file. Standard output is generally used for "normal" output from a command.


1 Answers

On Linux,

cat script.sh | bash /dev/stdin arguments 

seems to work.

like image 193
Michael Hoffman Avatar answered Oct 07 '22 17:10

Michael Hoffman