Execute bash script from URL

LinuxBashCurl

Linux Problem Overview


Say I have a file at the URL "http://mywebsite.com/myscript.txt" that contains a script:

#!/bin/bash
echo "Hello, world!"
read -p "What is your name? " name
echo "Hello, ${name}!"

And I'd like to run this script without first saving it to a file. How do I do this?

Now, I've seen the syntax:

bash < <(curl -s http://mywebsite.com/myscript.txt)

But this doesn't seem to work like it would if I saved to a file and then executed. For example readline doesn't work, and the output is just:

$ bash < <(curl -s http://mywebsite.com/myscript.txt)
Hello, world!

Similarly, I've tried:

curl -s http://mywebsite.com/myscript.txt | bash -s --

With the same results.

Originally I had a solution like:

timestamp=`date +%Y%m%d%H%M%S`
curl -s http://mywebsite.com/myscript.txt -o /tmp/.myscript.${timestamp}.tmp
bash /tmp/.myscript.${timestamp}.tmp
rm -f /tmp/.myscript.${timestamp}.tmp

But this seems sloppy, and I'd like a more elegant solution.

I'm aware of the security issues regarding running a shell script from a URL, but let's ignore all of that for right now.

Linux Solutions


Solution 1 - Linux

source <(curl -s http://mywebsite.com/myscript.txt)

ought to do it. Alternately, leave off the initial redirection on yours, which is redirecting standard input; bash takes a filename to execute just fine without redirection, and <(command) syntax provides a path.

bash <(curl -s http://mywebsite.com/myscript.txt)

It may be clearer if you look at the output of echo <(cat /dev/null)

Solution 2 - Linux

This is the way to execute remote script with passing to it some arguments (arg1 arg2):

curl -s http://server/path/script.sh | bash /dev/stdin arg1 arg2

Solution 3 - Linux

For bash, Bourne shell and fish:

curl -s http://server/path/script.sh | bash -s arg1 arg2

Flag "-s" makes shell read from stdin.

Solution 4 - Linux

Use:

curl -s -L URL_TO_SCRIPT_HERE | bash

For example:

curl -s -L http://bitly/10hA8iC | bash

Solution 5 - Linux

Using wget, which is usually part of default system installation:

bash <(wget -qO- http://mywebsite.com/myscript.txt)

Solution 6 - Linux

Try just:

bash <(curl -s http://mywebsite.com/myscript.txt)

Solution 7 - Linux

You can also do this:

wget -O - https://raw.github.com/luismartingil/commands/master/101_remote2local_wireshark.sh | bash

Solution 8 - Linux

The best way to do it is

curl http://domain/path/to/script.sh | bash -s arg1 arg2

which is a slight change of answer by @user77115

Solution 9 - Linux

I often using the following is enough

curl -s http://mywebsite.com/myscript.txt | sh

But in a old system( kernel2.4 ), it encounter problems, and do the following can solve it, I tried many others, only the following works

curl -s http://mywebsite.com/myscript.txt -o a.sh && sh a.sh && rm -f a.sh

Examples

$ curl -s someurl | sh
Starting to insert crontab
sh: _name}.sh: command not found
sh: line 208: syntax error near unexpected token `then'
sh: line 208: ` -eq 0 ]]; then'
$

The problem may cause by network slow, or bash version too old that can't handle network slow gracefully

However, the following solves the problem

$ curl -s someurl -o a.sh && sh a.sh && rm -f a.sh
Starting to insert crontab
Insert crontab entry is ok.
Insert crontab is done.
okay
$

Solution 10 - Linux

Also:

curl -sL https://.... | sudo bash -

Solution 11 - Linux

Just combining amra and user77115's answers:

wget -qO- https://raw.githubusercontent.com/lingtalfi/TheScientist/master/_bb_autoload/bbstart.sh | bash -s -- -v -v

It executes the bbstart.sh distant script passing it the -v -v options.

Solution 12 - Linux

Is some unattended scripts I use the following command:

sh -c "$(curl -fsSL <URL>)"

I recommend to avoid executing scripts directly from URLs. You should be sure the URL is safe and check the content of the script before executing, you can use a SHA256 checksum to validate the file before executing.

Solution 13 - Linux

instead of executing the script directly, first download it and then execute

SOURCE='https://gist.githubusercontent.com/cci-emciftci/123123/raw/123123/sample.sh'

curl $SOURCE -o ./my_sample.sh
chmod +x my_sample.sh
./my_sample.sh

Solution 14 - Linux

This way is good and conventional:

17:04:59@itqx|~
qx>source <(curl -Ls http://192.168.80.154/cent74/just4Test) Lord Jesus Loves YOU
Remote script test...
Param size: 4

---------
17:19:31@node7|/var/www/html/cent74
arch>cat just4Test
echo Remote script test...
echo Param size: $#

Solution 15 - Linux

If you want the script run using the current shell, regardless of what it is, use:

${SHELL:-sh} -c "$(wget -qO - http://mywebsite.com/myscript.txt)"

if you have wget, or:

${SHELL:-sh} -c "$(curl -Ls http://mywebsite.com/myscript.txt)"

if you have curl.

This command will still work if the script is interactive, i.e., it asks the user for input.

Note: OpenWRT has a wget clone but not curl, by default.

Solution 16 - Linux

bash | curl http://your.url.here/script.txt

actual example:

juan@juan-MS-7808:~$ bash | curl https://raw.githubusercontent.com/JPHACKER2k18/markwe/master/testapp.sh


Oh, wow im alive


juan@juan-MS-7808:~$ 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTristanView Question on Stackoverflow
Solution 1 - LinuxgeekosaurView Answer on Stackoverflow
Solution 2 - LinuxAnandi DasView Answer on Stackoverflow
Solution 3 - Linuxuser77115View Answer on Stackoverflow
Solution 4 - LinuxKatie MCView Answer on Stackoverflow
Solution 5 - LinuxamraView Answer on Stackoverflow
Solution 6 - LinuxRandom832View Answer on Stackoverflow
Solution 7 - LinuxluismartingilView Answer on Stackoverflow
Solution 8 - LinuxVamsidhar MuggullaView Answer on Stackoverflow
Solution 9 - LinuxChinglin WenView Answer on Stackoverflow
Solution 10 - LinuxThomas DecauxView Answer on Stackoverflow
Solution 11 - LinuxlingView Answer on Stackoverflow
Solution 12 - LinuxMauricio SánchezView Answer on Stackoverflow
Solution 13 - LinuxemrcftciView Answer on Stackoverflow
Solution 14 - LinuxjavafootView Answer on Stackoverflow
Solution 15 - LinuxRoss Smith IIView Answer on Stackoverflow
Solution 16 - LinuxRandom memes 2k18View Answer on Stackoverflow