How to Write a Shell Script With Input

A shell is the user environment in the Linux or Unix operating system that allows the user to interact with the operating system. It can be the graphical desktop or the command line. The shell is also the command interpreter for these operating systems. A shell script is a text file that contains a sequence of commands to be performed by the operating system. Shell scripts make it possible to execute a complex sequence of commands without typing the commands at the command prompt. It also allows users to perform the same sequence of commands multiple times without retyping the sequence.

Instructions

    • 1

      Open a blank document in any text editor.

    • 2

      Type the line "#!/bin/sh" to start the shell script. This line tells the operating system to use the bash shell. The bash shell is the default shell on most Linux distributions. It is commonly installed on many Unix operating systems.

    • 3

      Type the line "echo "Type your name and press Enter:"" to output the line "Type your name and press Enter:" to the screen.

    • 4

      Type the line "read name" to receive the input from the user, and place it in the variable called "name."

    • 5

      Type the line "echo "Hello $name!" to output the line "Hello <name typed into the keyboard>!" to the screen.

    • 6

      Save the file with the ".sh" file extension.

    • 7

      Type the command "chmod +x <script_name>.sh" at the command prompt to make the script executable. Replace "<script_name>" with the name of your file.

    • 8

      Type the command "./<script_name>.sh" to execute the script.

Related Searches:

References

Comments

You May Also Like

Related Ads

Featured