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
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.
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 …
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.
On Linux,
cat script.sh | bash /dev/stdin arguments
seems to work.
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