Shell Tutorial

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

Shell refers to an application program that provides an interface through which users 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 Tools


Shell Scripts

A shell script is a script program written for the shell.

When the industry says "shell", it usually refers to shell scripts, but readers should know that shell and shell script are two different concepts.

For the sake of habit and brevity, "shell programming" in this article refers to shell script programming, not developing the shell itself.


Shell Environment

Shell programming is like JavaScript and PHP programming: you only need a text editor to write code and a script interpreter to interpret and execute it.

There are many kinds of Shell in Linux; common ones include:

  • 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, that is, 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 of most Linux systems.

In general, people do not distinguish between Bourne Shell and Bourne Again Shell, so, like#!/bin/shit can also be changed to#!/bin/bash。

#!tells the system that the program specified by the path after 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 test.sh with the extension sh (sh stands for shell). The extension does not affect script execution; it is just for naming convenience. If you write shell scripts in PHP, just use the extension php.

Enter some code. The first line is generally like this:

Example

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

Run Example »

#!is a conventional marker that tells the system which interpreter this script needs to execute, that is, 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  #使脚本具有执行权限
./test.sh  #执行脚本

Note that you must write it as./test.shrather thantest.sh. The same applies to running other binary programs. If you directly write test.sh, the Linux system will search PATH for a file named test.sh, and only /bin, /sbin, /usr/bin, /usr/sbin, etc. are in PATH. Your current directory is usually not in PATH, so writing test.sh will result in "command not found"; you need to use ./test.sh to tell the system to look in the current directory.

2. As an interpreter argument

This way of running is to execute the interpreter directly, with the argument being the shell script file name, for example:

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

Scripts run in this way do not need to specify interpreter information on the first line; it is useless even if you write it.

Other Extensions