Switch
Switch

Reputation: 5366

Run a script piped from stdin (Linux/Shell Scripting)

Suppose I have a script: my_script.sh

Instead of doing

./my_script.sh

I want to do something like:

cat my_script.sh | <some command here>

such that the script executes. Is this possible?

The use case is if the script I want to execute is the output of a wget or s3cat, etc. Right now I save it to a temporary file, change it to executable, and then run it. Is there a way to do it directly?

Upvotes: 20

Views: 34972

Answers (3)

Mihai
Mihai

Reputation: 2155

You could use stdin from pipe:

cat my_script.sh | xargs -i <some_command> {}

or:

cat my_script.sh | bash -

or (just from stdin):

bash < my_script.sh

Upvotes: 6

l0b0
l0b0

Reputation: 58808

RVM recommends this method to run their installer:

bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)

Upvotes: 0

Mat
Mat

Reputation: 206689

Just pipe it to your favorite shell, for example:

$ cat my_script.sh
set -x
echo hello
$ cat my_script.sh | sh
+ echo hello
hello

(The set -x makes the shell print out each statement it is about to run before it runs it, handy for debugging, but it has nothing to do with your issue specifically - just there for demo purposes.)

Upvotes: 26

Related Questions