4

I want to run a script from GitHub with curl, and I can do this easily with:

# This is safe to run but will add a few lines to .bashrc, .vimrc, .inputrc to add some configuration options.
# Please check the project before running:
# https://raw.github.com/roysubs/custom_bash/
# But this is just to illustrate the point, bash shell script on GitHub, and how to use curl to pipe it to bash
curl -s https://raw.githubusercontent.com/roysubs/custom_bash/master/custom_loader.sh | bash

I have a few problems with this however that I was hoping for some help with:

  • Firstly, the script always runs unattended, ignoring all read -e -p statements to take input from the console. I'd appreciate if there is a better way to run the script such that it does not do this. Other methods to do this would be great.
  • Secondly, I used git.io to shorten the above url to https://git.io/Jt0fZ but I can never get the above curl method to work using this. i.e. curl -s https://git.io/Jt0fZ | bash Does anyone know why this fails and for a workaround to use the shortened url to execute the remote script?

1 Answer 1

4

The "Firstly" is more complex, but you might try a temporary shell script without using pipe.

curl -s https://raw.githubusercontent.com/roysubs/custom_bash/master/custom_loader.sh >tmp.sh

bash tmp.sh

Your "Secondly" question is very easy to explain, when you do curl -s https://git.io/Jt0fZ, we get a Redirect 302, here is hints:

We try to get headers only from the server:

curl --head https://git.io/Jt0fZ

HTTP/1.1 302 Found
Server: Cowboy
Connection: keep-alive
Date: Sun, 25 Apr 2021 14:24:43 GMT
Status: 302 Found
Content-Type: text/html;charset=utf-8
Location: https://raw.githubusercontent.com/roysubs/custom_bash/master/custom_loader.sh
Content-Length: 0
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Runtime: 0.003242
X-Node: 69d60833-56c5-4364-94c1-6f0c5b12863f
X-Revision: 392798d237fc1aa5cd55cada10d2945773e741a8
Strict-Transport-Security: max-age=31536000; includeSubDomains
Via: 1.1 vegur

We see HTTP/1.1 302 Found and Location: https://raw.githubusercontent.com/roysubs/custom_bash/master/custom_loader.sh, not the real content of the latter.

So bash gets empty script to run, by default, curl does not do a second call to the new location, as does graphic webbrowsers usually do.

But you can do this to overcome the default:

curl -L https://git.io/Jt0fZ | bash

man curl gives this:

-L, --location (HTTP) If the server reports that the requested page has moved to a different location (indicated with a Location: header and a 3XX response code), this option will make curl redo the request on the new place.

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

2 Comments

@Lawrence Cherone Yes thanks, i've added this at the end.
This is great, thanks. I've been scratching my head about these for quite a long time. For (1), curl [bla] > tmp.sh ; bash tmp.sh should work fine as you say, but is two commands. If there is no way around this, I can work with that of course (but great if someone knows a one-step solution). For (2), I see that the -L option works great (curl has so many options that my head breaks whenever I look at them!) 🙂

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.