Linux Shell Programming: Creating Your First Shell Script
Tags: linux, shell, script
Published: 2023-05-15 13:59:33

---
title: Linux Shell Programming: Creating Your First Shell Script
published: true
date: 2022-06-27 11:09:07 UTC
tags: linux, shell, script
series: Linux Cheatsheets
canonical_url: https://hayatscodes.hashnode.dev/linux-shell-programming-creating-your-first-shell-script-180071813dec
---

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](https://cdn.hashnode.com/res/hashnode/image/upload/v1672561002627/-Enb2cJm5.jpeg)

Photo by [AltumCode](https://unsplash.com/es/@altumcode?utm_source=medium&utm_medium=referral) on [Unsplash](https://unsplash.com?utm_source=medium&utm_medium=referral)

### **The .sh Extension**

Creating a **.sh** file enables the shell to identify files that are shell scripts.

### **The Shebang Construct**

![The shebang construct](https://cdn.hashnode.com/res/hashnode/image/upload/v1672561004016/3eCMaN6WX.png)

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](https://medium.com/@codingmaths/bin-bash-what-exactly-is-this-95fc8db817bf).

### **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**

![The LInux chmod command](https://cdn.hashnode.com/res/hashnode/image/upload/v1672561005609/SAPeJwmJn.png)

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](https://www.howtogeek.com/437958/how-to-use-the-chmod-command-on-linux/).

### **Example Script**

![Creating a Shell script that prints hello world](https://cdn.hashnode.com/res/hashnode/image/upload/v1672561006847/FP-9snU70.png)

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:

1. I created a hello file with the touch command.

2. Then I write into it using the cat and redirection commands.

3. I input the shebang construct to tell the shell that its a shell script since the file was not created with a _.sh extension._

4. Then I input _echo_, a command that prints text to the standard output, followed by the Hello World text in quotes.

5. I used the `chmod u+x` command to make the file executable.

6. 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](https://www.tutorialspoint.com/unix/shell_scripting.htm).