DEV Community

Alexander Omorokunwa
Alexander Omorokunwa

Posted on • Originally published at fossnaija.com on

Automating Stuffs: Introduction to Linux Shell Scripting

automate-shell-script-banner
Shell Script Banner – fossnaija.com

Inrecent times many computer users are comfortable usingpoint-and-click (graphical) user interfaces – GUI– and so any mention of commands becomes scary and disturbing. But asa Linuxuserthe command line interface(CLI) is an integral part of the operatingsystem(OS) and also very necessarily productivity or performance-wise.There still many operations that can be best carried out using theshell alone. Over the years most of our articles on fossnaijahavefeatured prominent command solutions to many Linux configurations andsettings. So in this post we are going to talk about a very usefulcommand-line feature of Linux – SHELL SCRIPTING.

TheLinux command line, also known as the shell,is an essential part of the OS. The shell is beyond a simple CLI; ithas many advance features that can enable you to interact with Linuxdevoid of a GUI. Among these features is a well defined (programming)language syntax that you can use to carry out programming activitieson the OS. Using this programming syntax; variables can be defined,assigned values, saved in files (called shell scripts) and executedlike any other command line program.

Forinstance if you have a sequence of Linuxcommands(instructions) that you use frequently you can store them in a shellscript. Then it is possible to have the shell read the script fileand execute the commands contained in it.

Solets see how you can create and execute a simple shell script usingbash (the BourneAgain SHell)which is one of the most popular Linux shells.

CREATEA SHELL SCRIPT

To create a shell script you need to write it in a text file using and of your favorite text-editor – like gedit, vi, nano, geany etc.

Type the following into the file;

 #!/bin/bash

 echo “Hello World!” 
Enter fullscreen mode Exit fullscreen mode

Then save the file as “hello” (without the “ ”).

EXECUTING THE SHELL SCRIPT

Change the file access permission (FAP) of the file, hello, to make it executable; using the following command:

chmod u+x hello

  1. Then you can execute then shell script using;

./hello

Then you’d get an _ Hello World! _ Printed on the terminal.

Whatjust happened?

  • The #! characters should be the first two characters of any script followed by the interpreter to execute it; in this case the /bin/bash – since we’re using the bash shell. If you want the /bin/ksh to be the script interpreter you should write #!/bin/ksh at the beginning of the script.

  • Theecho command used to display messages on the screen. In this casethe echo command displays the text enclosed between the doublequotes (“ ”), HelloWorld!,on the screen.

In later posts in this series we’d see some common Linux tasks we can automate using shell scripts. But for now churn on what you just learnt about shell scripts.

HappyLinux’NG!

The post Automating Stuffs: Introduction to Linux Shell Scripting appeared first on Foss Naija.

Top comments (0)