In this tutorial, you will learn how to run a shell script in Ubuntu using the native command line terminal. If you’ve recently taken up the art of Bash scripting or you’re just trying to figure out how to run a Linux-based shell script you found on the Internet, this walkthrough is for you. Oh, and this will also work for essentially any other Linux OS including Debian, Fedora, Mint, Arch Linux, MX Linux, and so on.
Contents
What are shell scripts, exactly?
A shell script is a file — usually ending in .sh (e.g. “script.sh”) — that consists of UNIX commands to be run through the terminal shell when you execute it. In Ubuntu, the default terminal shell is called Bash, and it’s what you use whenever you enter a command into the terminal.
You can tell whether a file is intended to be run as a shell script by the presence of what’s called a “shebang” on the first line: either #!/bin/bash
or #!/bin/sh
. The shebang tells the Ubuntu kernel which interpreter to use for the commands, given that /bin/bash
points to Bash and /bin/sh
points to Dash.
Why are they useful?
Let’s say you regularly need to type in several terminal commands one after another for some reason. Instead of typing out each command every time, it makes more sense to put them into a file that can be run all at once, executing each command in the order that you placed them. That’s what a shell script does in a nutshell, excuse the pun.
To give you an idea of what this looks like, here’s an example of a (completely useless) Bash script that creates a new file, adds some text to the file, outputs the file contents on screen, and then removes the file as if nothing ever happened:
#!/bin/bash # The echo command prints a message to the terminal. echo "Initializing script..." # The touch command creates a new empty file. touch newfile.txt # Here, the echo command prints a message to newfile.txt, by making use of the > symbol. echo "This file will self-destruct after displaying this message." > newfile.txt # The cat command displays the contents of a file in the terminal. cat newfile.txt # The rm command removes a file or folder. rm newfile.txt echo "Entirely useless script completed!"
Beginner’s tip: to quickly open a shell script in Ubuntu (e.g. any file ending with .sh) and read its code, you can use the
cat
command followed by the file name. For example:
cat script.sh
Or you can use the
less
reader in the same manner (press Q to exit theless
reader when done):
less script.sh
The beauty of shell scripting is that you can easily automate all sorts of tasks on your system without having to learn much in the way of actual programming. There’s a lot you can accomplish with even the most basic Ubuntu commands.
And as easy as it is to learn, shell scripting can also be very powerful. For example, with only a few dozen commands (plus the magic of variables and programming basics like if statements), you can install and configure a specialized web server or a highly customized Linux-based platform from scratch and with no manual input. All you have to do is run the script, and away she goes!
Which brings us to the point of this tutorial: how to run a shell script in Ubuntu.
Step 1: Make the file executable
In your terminal (shortcut to open a new terminal: Ctrl+Alt+T), navigate with cd
to the directory where the shell script file is located:
cd /path/to/the/script.sh
Next, make sure the script has permission to execute:
chmod a+x script.sh
If you want the file to be executable by your user only, replace chmod a+x
with chmod u+x
.
Step 2: Run the script file
Option 1: To run the file, simply enter the filename with its filepath into your terminal. Since you are already in the file’s directory, you can use “.” (full stop) as a placeholder for the current directory:
./script.sh
Option 2: An alternative method is to call the script by using Bash directly:
bash script.sh
And that’s all there is to it! If you encounter any problems with executing shell scripts in Ubuntu, let us know what you’re struggling with in the comments.
Tom Davis is a technical contributor at TechWombat. He enjoys writing on IT, open source, electronics, and other geeky arcana. Tom’s always happy to reply to comments and corrections, so be nice and send him your thoughts at tomdavis@techwombat.com or in the comment section below.
Thanks! CTRL-ALT-T invokes the Terminal, btw. Had to look that up first, lol.
A useful shortcut, thanks. It has been added to the tutorial.
Pinning the terminal app to the Favorites bar is another good idea if you’re going to be using it a lot.