18

Say this I have the following file my_cool_file.sh in a Git repository in Github (or BitBucket for that matter) named my_cool_repo. The file is a script used to install ConfigServer's well known CSF-LFD software:

#!/bin/bash
cd /usr/src
rm -fv csf.tgz
wget https://download.configserver.com/csf.tgz
tar -xzf csf.tgz
cd csf
sh install.sh
sed -i "s/TESTING = "1"/TESTING = "0"/g" /etc/csf/csf.conf
csf -r
perl /usr/local/csf/bin/csftest.pl
# sh /etc/csf/uninstall.sh

How can one execute this Bash script (a .sh file) directly from Github, via command line?

1
  • 2
    As you are putting this script up on github, consider #!/usr/bin/env bash as your hash-bang for better portability.
    – Mark
    Jan 5, 2018 at 0:45

4 Answers 4

27

Load the file (make sure you use the raw file, otherwise you load the HTML page!) with wget using its exact URL and then pipe the output to bash:

Here's an example with clarifications:

wget -O - https://raw.githubusercontent.com/<username>/<project>/<branch>/<path>/<file> | bash

From the manpage for the wget command:

   -O file
   --output-document=file
       The documents will not be written to the appropriate files, but all
       will be concatenated together and written to file.  If - is used as
       file, documents will be printed to standard output, disabling link
       conversion.  (Use ./- to print to a file literally named -.)

       Use of -O is not intended to mean simply "use the name file instead
       of the one in the URL;" rather, it is analogous to shell
       redirection: wget -O file http://foo is intended to work like wget
       -O - http://foo > file; file will be truncated immediately, and all
       downloaded content will be written there.

So outputting to - will actually write the files content to STDOUT and then you simply pipe it to bash or whatever shell you prefer. If you script needs sudo rights you need to do sudo bash at the end so the line becomes:

wget -O - https://raw.githubusercontent.com/<username>/<project>/<branch>/<path>/<file> | sudo bash
1
  • 2
    What about params? Let's say the piped script optionally handles $1, would there be a way to pass it in?
    – Wu Wei
    Jan 15, 2020 at 16:41
7

If your script has user entries like read user_input try this:

bash <(curl -s https://raw.githubusercontent.com/username/project/branch/path/file.sh)

The -s parameter don't show download progress.

0
0

You may use following command to pass in the parameters

bash -s

Found it here: passing parameters to bash when executing a script fetched by curl

0

If you want your bash script to be able to use pipes, use bash -c:

cat myfile.txt | \
bash -c "$(curl http://example.com/script.sh )" -s arg1 arg2

You can also use it with wget -O (as in @videonauth 's answer)

Example usage:

#!/usr/bin/env bash

export MYURL="https://raw.githubusercontent.com/sohale/snippets/master/bash-magic/add-date.sh"
curl http://www.google.com | \
bash -c "$(curl -L $MYURL )" -s "       >>>>> next line 🕶👉"

If you use bit.ly to shorten the url, don't forget the -L.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .