1. Overview

Various installation guides in Linux involve running a shell script first, which we download and then execute separately using Bash. Not only installation guides but also custom shell scripts written for automating manual activities involve downloading and executing scripts.

In this tutorial, we’ll cover a way using curl and wget to download and execute a script in a single step.

2. Installation and Usage – curl

Some Linux distributions may not have curl installed by default.

We can install it on any Ubuntu/Debian system using apt-get:

[email protected]:~$ sudo apt-get install curl

Or we can similarly install it on CentOs/Fedora/Redhat:

[email protected]:~$ sudo yum install curl

Now, to download and execute a script with cURL, we can use the -s option. Then, we pipe the result to bash:

[email protected]:~$ curl -s http://example.com/script.sh | bash

When we use the -s option, we’re asking cURL to get all content at the URL silently which doesn’t display statistics like average download speed and total time.

This enables us to use a pipe | and feed it to the Bash command which will actually execute the script.

3. Installation and Usage – wget

Most Linux distributions have wget installed by default.

We can install it on any Ubuntu/Debian system using apt-get:

[email protected]:~$ sudo apt-get install wget

Or we can similarly install it on CentOs/Fedora/Redhat:

[email protected]:~$ sudo yum install wget

Now, to download and execute a script with wget, we can use the -q0 options, a hyphen, and then pipe the result to bash:

[email protected]:~$ wget -qO - http://example.com/script.sh | bash

When we use the -q option, we are telling wget to quietly download the file by not printing its verbose output.

The option O is used to save the content of script.sh to a file. The trailing hyphen (the one between -qO and http://) is to tell wget to direct this content to /dev/stdout. Anything directed to /dev/stdout is displayed right on the terminal, that’s the reason why the content of script.sh is never written to disk.

This trailing hyphen enables us to pipe the content of script.sh to the bash command which actually executes it. If we use wget without a trailing hyphen and capital O, then get will save the content of the file to disk with the filename script.sh. This is because wget is actually used to download files in Linux.

On the other hand, curl will never download files by default. Although, we’ve just seen how we can use it that way.

 4. Conclusion

In this short tutorial, we learned how to execute a shell script directly from a URL using curl and wget.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments