How to write your first shell script

How to write your first shell script

Launch a terminal vi, nano or any other prefered editor of your choice. I will be using nano as a editor.

nano /home/bash-scripts/my-first-script.sh

At the top type the following code #!/bin/bash. This is known as a Shebang line.

#!/bin/bash
echo "start of script"
ls
echo "end of script"

This script prints a header line then list directories and a footer message in script.
Now to come out of the nano editor, press escape and save changes.
Now we need to make this Shell Script, executable. To do this, enter the following command

chmod +x /home/bash-scripts/my-first-script.sh

To run this Shell Script, simply type

sh /home/bash-scripts/my-first-script.sh

You should get the list of directories
 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *