Write shell scripts

Let’s write a simple shell script. This script will execute a single echo command:

linux sample shell script

To run the script, we need to make it executable. We will use the chmod command to do this:

linux make script executable

Now we can run the script:

linux executing a shell script

We can execute more than one command in a script. Consider the following script:

linux sample script more commands

This script will display the current date after the text. Let’s run the script:

linux execute the script

You can use variables in a script. For example, consider the following script:

linux using variables in a shell script

The script above defines the variable name. The variable is then used in the echo command. This is the result:

linux sample script execute

You can also ask a user to enter a value for the variable. You do this using the read method, like in this example:

linux read method in a shell script

The code in the example above asks the user to enter his or her name. It then assigns the text that the user has entered to the variable name. The variable is then used in the echo statement. Here is the result:

linux read execute script

Subscribe to our Newsletter

Like us on Facebook

top
Geek University 2019