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.
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/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 echo "Initializing script..." touch newfile.txt echo "This file will self-destruct after displaying this message." > newfile.txt cat newfile.txt rm newfile.txt echo "Entirely useless script completed!"
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:
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
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:
Option 2: An alternative method is to call the script by using Bash directly:
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.
2 thoughts on “How to Run a Shell Script in Ubuntu via the Terminal”
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.