60

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

2 Answers 2

59

On Linux,

cat script.sh | bash /dev/stdin arguments

seems to work.

Sign up to request clarification or add additional context in comments.

6 Comments

Excellent! Would not have guessed it.
cat script.sh | bash -s - arguments
That's good, that would surely work everywhere, whereas /dev/stdin might not.
@ccarton Your comment is a better answer. For the sake of posterity, would you consider entering it again as an actual answer?
Since this is the accepted answer it would be great to edit it to use bash -s - @MichaelHoffman
|
45

After @ccarton's comment:

cat script.sh | bash -s - arguments

It's more portable than @Michael Hoffman's solution.

3 Comments

Thanks! This is really useful for automated setups via curl .. | bash -s ... Is the dash really needed? It seems that it works like this: cat script.sh | bash -s arguments
@Alek In this context (according to man bash), - is equivalent is equivalent to --, which is typically used to signal the end of options. So, if you've got an argument that bash could misinterpret as one of its options, you'll want -- to disambiguate.
works for sh (dash) as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.