Easy Tutorial
❮ Linux Comm Su Linux Comm Export ❯

Shell Tutorial

Shell is a program written in C language, serving as a bridge for users to use Linux. Shell is both a command language and a programming language.

Shell refers to an application that provides an interface through which users can access the services of the operating system kernel.

Ken Thompson's sh was the first Unix Shell, and Windows Explorer is a typical graphical interface Shell.

Shell Online Tool


Shell Script

A Shell script (shell script) is a script program written for the shell.

In the industry, shell usually refers to shell scripts, but readers should be aware that shell and shell script are two different concepts.

For the sake of brevity, the term "shell programming" in this article refers to shell scripting, not the development of the shell itself.


Shell Environment

Shell programming, like JavaScript and PHP programming, only requires a text editor for writing code and a script interpreter for execution.

There are many types of Linux Shells, some common ones include:

This tutorial focuses on Bash, the Bourne Again Shell, which is widely used in daily work due to its ease of use and free availability. Bash is also the default Shell on most Linux systems.

Usually, people do not distinguish between Bourne Shell and Bourne Again Shell, so #!/bin/sh can also be changed to #!/bin/bash.

#! tells the system that the program specified by the path following it is the Shell program that interprets this script file.


First Shell Script

Open a text editor (you can use the vi/vim command to create a file), create a new file named test.sh, with the extension sh (sh stands for shell). The extension does not affect script execution; it's just a good practice to name it appropriately. If you use PHP to write shell scripts, use the .php extension.

Enter some code, typically the first line looks like this:

Example

#!/bin/bash
echo "Hello World !"

#! is a convention that tells the system which interpreter to use to execute the script, i.e., which Shell to use.

The echo command is used to output text to the window.

There are two ways to run a Shell script:

1. As an executable program

Save the above code as test.sh and cd to the corresponding directory:

chmod +x ./test.sh  # Make the script executable
./test.sh  # Execute the script

Note, it must be written as ./test.sh, not test.sh. When running other binary programs, the same rule applies. Simply writing test.sh will cause the system to look for it in the PATH, and typically only directories like /bin, /sbin, /usr/bin, /usr/sbin, etc., are in the PATH. Your current directory is usually not in the PATH, so writing test.sh will not find the command. Use ./test.sh to indicate that it should be found in the current directory.

2. As an interpreter parameter

This method involves directly running the interpreter with the shell script filename as a parameter, for example:

/bin/sh test.sh
/bin/php test.php

Scripts run this way do not need to specify the interpreter information on the first line, and doing so would be ineffective.

❮ Linux Comm Su Linux Comm Export ❯