Shell Tutorial


Shell is a program written in C language. It is 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 access the services of the operating system kernel.

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

Shell Online Tool


Shell Script

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

The shell mentioned in the industry usually refers to shell script, but readers should know that shell and shell script are two different concepts.

Due to habit and brevity, the "shell programming" appearing in this article refers to shell script programming, not to the development of the shell itself.


Shell environment

Shell programming is the same as java and php programming. As long as there is a text editor that can write code and a script interpreter that can interpret and execute it.

Linux has many types of Shells, the common ones are:

  • Bourne Shell (/usr/bin/sh or /bin/sh)

  • Bourne Again Shell (/bin/bash)

  • C Shell (/usr/bin/csh)

  • K Shell (/usr/bin/ksh)

  • ##Shell for Root (/sbin/sh)

  • ......

This tutorial focuses on Bash, also known as Bourne Again Shell. Because it is easy to use and free, Bash is widely used in daily work. At the same time, Bash is also the default Shell for most Linux systems.

In general, people do not distinguish between Bourne Shell and Bourne Again Shell, so like

#!/bin/sh, it can also be changed to#!/ bin/bash.

#! Tell the system that the program specified by the subsequent path is the Shell program that interprets this script file.


The first shell script

Open the text editor (you can use the vi/vim command to create the file), create a new file test.sh with the extension sh (sh stands for shell), the extension does not affect script execution, just know the name and understand it. If you use PHP to write shell scripts, just use PHP as the extension.

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

Instance

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

Run Instance» Click "Run Instance" " button to view online examples

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 #使脚本具有执行权限 ./test.sh #执行脚本

Note that it must be written as ./test.sh instead of test.sh. The same goes for running other binary programs. Write test.sh directly. The Linux system will go to PATH to find if there is one called test.sh. , and only /bin, /sbin, /usr/bin, /usr/sbin, etc. are in PATH. Your current directory is usually not in PATH, so if you write test.sh, you will not be able to find the command. You need to use ./test .sh tells the system to look for it in the current directory.


#2. As an interpreter parameter

This mode of operation is to run the interpreter directly, and its parameter is the file name of the shell script, such as:

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

A script run in this way does not need to specify the interpreter information on the first line, and it is useless if it is written.