GIDNetwork > A basic Bash Script
« My first BASH Script. Multipart Articles on GIDNetwork »

A basic Bash Script

by: admin - Dec 19, 2005

Today marks the second day since I wrote my first Bash script. Here are some of my notes put together so it may help someone like me in the future, someone who has never written a Bash script before in his/her life!

Basic Bash Script

Often, you will start a BASH script with a shebang line: #!/bin/bash, assuming BASH is located at /bin. Also, it's common to "end" the script with exit [n].

Depending on how you write and use your script, both are optional of course.

Generic Code Example:


# script prints out some text to the screen. 
echo "My first Bash Script!"

exit 0

Bash Comments

As you may have already guessed, comments in Bash scripts are similar to other programming languages i.e. anything starting with the hash / pound sign (#), is considered a single line comment and ignored by the command interpreter. Everywhere, except in the shebang line at the top of the file, if it exists.

Bash Script Executable

As soon as you have finished writing your Bash script, you will have to make it executable. Say you saved the file (script) as my_script and you are inside the folder where this file is located:

Generic Code Example:

chmod u+x ./my_script

Running / Executing a Bash Script

This may seem straight-forward but it's not. If you are inside the folder where your script resides, and you enter:

Generic Code Example:


you'd probably get an error like -bash: my_script: command not found and you'd wonder why.

Instead, you should type & enter:

Generic Code Example:


or use an absolute path like:

Generic Code Example:

Would you like to comment? This story has been viewed 24,557 times.
« My first BASH Script. Multipart Articles on GIDNetwork »


Copyright © GIDNetwork™ 2001 - 2021

Another website by J de Silva

Page generated in : 0.00663 sec.