A shell, in basic terms, is a Linux command-line interface that accepts input, executes it, and displays it on the standard output. The input could be from the user (keyboard) or redirected from a file. And the user screen is the standard output (it can also be redirected).
A shell script is a computer program that executes commands sequentially (line by line) and tells the shell environment what to do and when to do it. It can be used to perform simple operations like file manipulation and text printing, as well as complex programs like file test operators and string test operators.
In this tutorial, we will be creating a simple shell script that prints Hello World! to the standard output.
Requirements: Bash or Bourne shell.
Basic Concept
Photo by AltumCode on Unsplash
The .sh Extension
Creating a .sh file enables the shell to identify files that are shell scripts.
The Shebang Construct
The shebang construct tells the shell that a file is a shell script and directs it to the path where the bash shell interpreter is located so that the script can be executed.
This allows the user to create a shell script file without the usual .sh extension.
The construct is placed in the first line of a bash shell script. It is called a shebang because of the # (hash) and ! (bang) symbols.
The above construct points to the bin/bash file. But the path is different for every shell type. For example, the bourne shell (sh) uses bin/sh.
For more on the Shebang construct, click here.
Commands To Be Executed
The commands that follow the shebang construct or those that are found in a .sh file are the programs that are executed by the shell. The commands could be in a single line or multiple lines. But it would always be executed line by line, from top to bottom.
Making Your Script Executable
To run a script, the shell requires that the file be executable. By default, files in the shell can only be read into and written into. To make it executable, the chmod u+x
command followed by the file name is used.
The chmod
command, as the name implies, is used to change the mode of a file. It is usually followed by options that tell it what mode to change the file to, one of which is the u+x
option.
The u+x option stands for the user and executable, and this makes the file executable for the user only. For more on chmod
and its other options, check this out.
Example Script
Like in any other programming language, the Hello World! program is the first program we will write.
The above diagram is a program that prints Hello World! to the standard output.
The program is written in the following steps:
I created a hello file with the touch command.
Then I write into it using the cat and redirection commands.
I input the shebang construct to tell the shell that its a shell script since the file was not created with a .sh extension.
Then I input echo, a command that prints text to the standard output, followed by the Hello World text in quotes.
I used the
chmod u+x
command to make the file executable.and executed the program with ./hello.
The above steps apply to files without the .sh
extension. To create a similar program in a .sh extension file (i.e. hello.sh), only write the echo command followed by the text into the file._
Conclusion
shell scripting is a lot more complex than this. As the following proverb states, a journey of a thousand miles begins with a step. In no time, you'd be comfortable writing advanced shell scripts. To continue learning Linux shell programming, visit the tutorialspoint website.
Top comments (0)