# How to write a basic shell script

Learn how to write a basic Bash shell script. Although shell scripts can be long, complicated scripts which perform complex tasks, you can also learn how to write a simple shell script in 10 minutes. If you can run commands at the command line, learning how to write a basic Bash script is easy.

## Requirements

- A Cloud Server running Linux
- A basic familiarity with the command line

## Create the file

Go to your home directory:

```none
cd
```

---

### Note

Using cd without any arguments will automatically take you to your home directory.

---

Create a directory for test scripts:

```none
mkdir myscripts
```

Move into this directory:

```none
cd myscripts
```

Create a file named myscript.sh and open it for editing:

```none
nano hello
```

The first line of any shell script must be #! followed by the path to the shell that you want to use for the script.

Since we will be writing this script for the Bash shell, the first line of the file will be:

```none
#!/bin/bash
```

Any other line which begins with a hash mark # will be ignored by the script. This allows you to add comments and notes to your script. After the first line, it is a good practice to add a few comments about the script's purpose:

```none
# A test script
```

## Add the command(s) and make it executable

Any command which you run from the command line, you can put into a shell script. This is convenient for:

- Commands you use frequently.
- Lengthy commands, especially those which use lots of flags.
- Commands which you want the server to run automatically as a [cron job](https://en.wikipedia.org/wiki/Cron "cron job").
- Commands you can never quite remember, and have to look up each time.

For this example, we will use a command that will echo the words "Hello world" to the command line. Put the following into the file:

```none
echo "Hello world"
```

Save and exit the file.

The script needs to be executable in order to run. Give the script executable permissions with the command:

```none
chmod 755 hello
```

## Run the script

To run the script, simply invoke it by its file path. Since you are in the same directory as the script, you can run it from here by typing:

```none
./hello
```

You can run the script from anywhere on the server by typing the full file path:

```none
/home/[your username]/myscripts/hello
```

### Add the script to your PATH

Unlike your shell script, most Linux commands do not require you to type out the full file path each time you want to run them. This is because those scripts are in your PATH.

The PATH is the list of directories that the shell checks for a command each time you type it. You can see your PATH by using the command:

```none
echo $PATH
```

This will output the list of directories which are in your PATH:

```none
jdoe@localhost:~$ echo $PATH
/home/jdoe/bin:/home/jdoe/.local/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
```

There are two ways to add your shell script to your PATH:

**1. Move the shell script to a directory that is already in your PATH**

You can move the shell script into any of the directories which are listed in response to the echo $PATH command. The best practice is to put your personal shell scripts into the /usr/local/bin directory:

```none
sudo mv hello /usr/local/bin
```

You can then run the script from anywhere with the command:

```none
hello
```

**2. Add the shell script's directory to your PATH**

You can add any directory to your PATH with the command:

```none
export PATH=$PATH:[path to directory]
```

For example, if user jdoe wants to add /home/jdoe/myscripts to the PATH, the command is:

```none
export PATH=$PATH:/home/jdoe/myscripts
```

You can then run the script from anywhere with the command:

```none
hello
```

## Use input and variables

Your scripts can use variables, and you can prompt the user for input. This creates an interactive script which allows you to give it new information each time it is run.

Let's start by creating a new file with the name greetme and opening it for editing:

```none
nano greetme
```

As before, make sure that the first line of the script is the path to the shell:

```none
#!/bin/bash
```

Add a comment:

```none
# A simple script to experiment with user input
```

The first line of the script will be what the script says when you invoke it:

```none
echo -e "Hello user, please tell me which state you live in: "
```

---

### Note

We use echo -e for this, because -e allows the command to handle special characters.

---

The next line tells the script to accept your input at a prompt:

```none
read state
```

The final line is the script's response:

```none
echo "I have heard that $state is lovely this time of year."
```

The entire script reads as follows:

```none
#!/bin/bash
# A simple script to experiment with user input
echo -e "Hello user, please tell me which state you live in: "
read state
echo "I have heard that $state is lovely this time of year."
```

Save and exit the file. Then make it executable with the command:

```none
chmod 755 greetme
```

You can now run the file and see the results:

```none
jdoe@localhost:~/myscripts$ greetme
Hello user, please tell me which state you live in:
California
I have heard that California is lovely this time of year.
```


This is a markdown version of: [https://www.ionos.com/digitalguide/server/know-how/how-to-write-a-basic-shell-script/](https://www.ionos.com/digitalguide/server/know-how/how-to-write-a-basic-shell-script/) for AI/LLM consumption.