Write and run a bash file

This post introduces how to writ and run a bash file from terminal on Ubuntu. (If you prefer video style tutorials, check here for a post that is video based.)

The bash ( Bourne Again Shell) is the most common shell installed with Linux distributions and Mac OS.

  • Write a bash file

the most common is to write a file, make sure the first line is


Then save the file. Next mark it executable using chmod +x file

Then when you click (or run the file from the terminal) the commands will be executed. By convention these files usually have no extension, however you can make then end in .sh or any other way.

For example,

echo Hello World

A Simple Bash Example

echo "This is a shell script"  
ls -lah  
echo "I am done running ls"  
SOMEVAR='text stuff'  
echo "$SOMEVAR"  
  • Run a bash file

go to the folder where you bash file is located and type:


./ just means that you should call the script located in the current directory. (Alternatively, just type the full path of the yourbashfile.sh). If it doesn’t work then, check if yourbashfile.sh has execute permissions.

You can add execute permission by the following command:

$ chmod +x yourbashfile.sh


Why Bother?

Why do you need to learn the command line anyway? Well, let me tell you a story. A few years ago we had a problem where I used to work. There was a shared drive on one of our file servers that kept getting full. I won’t mention that this legacy operating system did not support user quotas; that’s another story. But the server kept getting full and it stopped people from working. One of our software engineers spent the better part of a day writing a C++ program that would look through all the user’s directories and add up the space they were using and make a listing of the results. Since I was forced to use the legacy OS while I was on the job, I installed a Linux-like command line environment for it. When I heard about the problem, I realized I could do all the work this engineer had done with this single line:

du -s * | sort -nr > $HOME/user_space_report.txt

Graphical user interfaces (GUIs) are helpful for many tasks, but they are not good for all tasks. I have long felt that most computers today are not powered by electricity. They instead seem to be powered by the “pumping” motion of the mouse! Computers were supposed to free us from manual labor, but how many times have you performed some task you felt sure the computer should be able to do but you ended up doing the work yourself by tediously working the mouse? Pointing and clicking, pointing and clicking.

I once heard an author say that when you are a child you use a computer by looking at the pictures. When you grow up, you learn to read and write. Welcome to Computer Literacy 101. Now let’s get to work.


  1. What Is “The Shell”?
  2. Navigation
  3. Looking Around
  4. A Guided Tour
  5. Manipulating Files
  6. Working With Commands
  7. I/O Redirection
  8. Expansion
  9. Permissions
  10. Job Control

Here Is Where The Fun Begins

With the thousands of commands available for the command line user, how can you remember them all? The answer is, you don’t. The real power of the computer is its ability to do the work for you. To get it to do that, we use the power of the shell to automate things. We write shell scripts.

What Are Shell Scripts?

In the simplest terms, a shell script is a file containing a series of commands. The shell reads this file and carries out the commands as though they have been entered directly on the command line.

The shell is somewhat unique, in that it is both a powerful command line interface to the system and a scripting language interpreter. As we will see, most of the things that can be done on the command line can be done in scripts, and most of the things that can be done in scripts can be done on the command line.

We have covered many shell features, but we have focused on those features most often used directly on the command line. The shell also provides a set of features usually (but not always) used when writing programs.

Scripts unlock the power of your Linux machine. So let’s have some fun!


  1. Writing Your First Script And Getting It To Work
  2. Editing The Scripts You Already Have
  3. Here Scripts
  4. Variables
  5. Command Substitution And Constants
  6. Shell Functions
  7. Some Real Work
  8. Flow Control – Part 1
  9. Stay Out Of Trouble
  10. Keyboard Input And Arithmetic
  11. Flow Control – Part 2
  12. Positional Parameters
  13. Flow Control – Part3
  14. Errors And Signals And Traps (Oh My!) – Part 1
  15. Errors And Signals And Traps (Oh My!) – Part 2



Ubuntu – Shell script to execute/run (pdf)

wikiHow to Write a Shell Script Using Bash Shell in Ubuntu

How to create & execute a script file [closed]

Advanced Bash-Scripting Guide (An in-depth exploration of the art of shell scripting) by Mendel Cooper