Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialZeeshan Dawood
7,210 PointsWhat does curl -L trhou.se/patch01 | bash do? Why did we do it in order to create a new user? I'm just curious.
What does curl -L trhou.se/patch01 | bash do? Why did we do it in order to create a new user? I'm just curious.
1 Answer
William Li
Courses Plus Student 26,868 PointsHi, Zeeshan Dawood
curl -L trhou.se/patch01 | bash
This command has 2 parts.
- the thing to the left of | --
curl -L trhou.se/patch01
- and the thing on the right of | --
bash
The | sign on UNIX command line is called pipeline, One of UNIX's design philosophy is "small tool that does one thing really well", and UNIX command line was made up of many of such small tools; and pipeline let you chain 2 or more of these tools together in a single command to do some very powerful thing.
Let's take a look at the 1st part.
curl -L trhou.se/patch01
The URL here trhou.se/patch01 is shorten, upon expanding, you'll see that the full URL http://treehouse-code-samples.s3.amazonaws.com/scripts/console-stage02-patch01.sh points to a .sh file, which is a shell script file.
You can try to just typing curl -L trhou.se/patch01
in the command line, curl gets this file, and display its content as textstream on your console; yeah, that's pretty all it does.
ยง curl -L trhou.se/patch01
#!/bin/bash
echo "==========================================================="
echo "= We are about to ask for your password ="
echo -e "= The default password is \033[38;5;81mmike the frog\033[0m ="
echo "==========================================================="
# This removes the user mike if exists
sudo -k
sudo -- sh -c "/bin/rm -r /home/mike ; /usr/sbin/deluser mike" &> /dev/null
echo "==========================================================="
echo "= Update Successful ! ="
echo "==========================================================="
It's cool that we can just use Command line to get a file from the net and display its content; but that alone isn't very useful, because we're dealing with a shell script file here, script file is supposed be executed.
And that's where the 2nd part of the command kicks in. bash command is used for executing shell script. For example, bash example.sh
will execute the example.sh script using the Bash Shell.
So to put everything together. In this command curl -L trhou.se/patch01 | bash
.
- curl get the script file from the URL
- through the power of UNIX pipeline |, the content of the the script file was passed as argument for the bash command to execute line by line.
Hope you find this useful. If you're interested on learning more about UNIX pipeline, check out this page. http://www.maketecheasier.com/pipes-redirection-for-linux-command-line/
Here's also a extremely well-written book on Command Line http://conqueringthecommandline.com/book
foss
7,601 Pointsfoss
7,601 Pointsthank you for this thorough explanation William Li ! Very helpful :)
William Li
Courses Plus Student 26,868 PointsWilliam Li
Courses Plus Student 26,868 PointsHi, Mary, I'm very glad that you find it helpful :)