Basic operation of Bourne shell: sh command and use in UNIX environment

Explanation of IT Terms

What is the Bourne Shell?

The Bourne shell, also known as “sh,” is a command-line interpreter or shell that provides a command-line interface (CLI) for interacting with the UNIX operating system. Developed by Stephen Bourne, it was the default shell in most UNIX systems until the introduction of the more advanced shells, such as the C shell (csh) and the Bourne Again shell (bash).

Basic Operation of the Bourne Shell

The Bourne shell allows users to execute commands and scripts, control processes, and manage files and directories. It provides a wide range of functionalities, such as file manipulation, variable handling, and control structures.

To use the Bourne shell, you need to open a terminal or command prompt in a UNIX environment and type “sh” followed by the desired command. Let’s take a look at some basic operations:

1. Running a Single Command
You can execute a single command by typing it directly after the “sh” command. For example, to list all the contents in a directory, you can use the “ls” command:

“`
sh ls
“`

2. Running a Shell Script
The Bourne shell supports the execution of shell scripts—plain text files containing a series of commands. To run a shell script, you need to provide the file name as an argument to the “sh” command. For example, assuming you have a script file named “myscript.sh,” you can run it using the following command:

“`
sh myscript.sh
“`

3. Redirecting Input and Output
The Bourne shell provides the ability to redirect input and output using special characters. The “>” character redirects the output of a command to a file, while the “<" character redirects input from a file. For example, to save the output of the "ls" command to a file named "output.txt," you can use the following command: ``` sh ls > output.txt
“`

4. Using Variables
The Bourne shell allows defining and using variables to store and manipulate data. Variables can be assigned values using the “=” operator. For example, to assign the value “Hello” to a variable named “greeting,” you can use the following command:

“`
greeting=”Hello”
“`

You can then retrieve the value of the variable by referencing it with a “$” sign:

“`
sh echo $greeting
“`

5. Handling Control Structures
The Bourne shell supports control structures such as loops and conditional statements. For example, you can use the “if” statement to execute a command conditionally:

“`
if [ $x -eq 5 ]; then
sh echo “x is equal to 5”
else
sh echo “x is not equal to 5”
fi
“`

Conclusion

The Bourne shell is a fundamental tool for interacting with the UNIX environment. It allows users to execute commands, run shell scripts, redirect input and output, handle variables, and implement control structures. Understanding the basic operations of the Bourne shell is essential for working effectively in UNIX systems. By becoming familiar with its capabilities, users can enhance their command-line skills and leverage the power of the UNIX operating system.

Reference Articles

Reference Articles

Read also

[Google Chrome] The definitive solution for right-click translations that no longer come up.