In recent times many computer users are comfortable using point-and-click (graphical) user interfaces – GUI – and so any mention of commands becomes scary and disturbing. But as a Linux user the command line interface (CLI) is an integral part of the operating system (OS) and also very necessarily productivity or performance-wise. There still many operations that can be best carried out using the shell alone. Over the years most of our articles on fossnaija have featured prominent command solutions to many Linux configurations and settings. So in this post we are going to talk about a very useful command-line feature of Linux – SHELL SCRIPTING.
The Linux command line, also known as the shell, is an essential part of the OS. The shell is beyond a simple CLI; it has many advance features that can enable you to interact with Linux devoid of a GUI. Among these features is a well defined (programming) language syntax that you can use to carry out programming activities on the OS. Using this programming syntax; variables can be defined, assigned values, saved in files (called shell scripts) and executed like any other command line program.
For instance if you have a sequence of Linux commands (instructions) that you use frequently you can store them in a shell script. Then it is possible to have the shell read the script file and execute the commands contained in it.
So lets see how you can create and execute a simple shell script using bash (the Bourne Again SHell) which is one of the most popular Linux shells.
CREATE A 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!”
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
2. Then you can execute then shell script using;
Then you’d get an Hello World! Printed on the terminal.
What just 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.
- The echo command used to display messages on the screen. In this case the echo command displays the text enclosed between the double quotes (“ ”), Hello World!, 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.
Latest posts by ALEXANDER WAYNE OMOROKUNWA (see all)
- How to Install FileZilla FTP Client on Linux - June 7, 2019
- Automating Stuffs: Introduction to Linux Shell Scripting - May 30, 2019
- Install GIMP If You Want PHOTOSHOP On Linux. - April 30, 2019