Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Shell tutorial


May 22, 2021 Linux


Table of contents


Shell tutorial

Shell is a program written in C, which 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 can 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

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

The industry usually refers to shell scripts, but readers and friends need to know that shell and shell script are two different concepts.

For customary reasons, concisely, the "shell programming" that appears in this article refers to shell script programming, not to the development of shell itself.


Shell environment

Shell programming, like java and php programs, requires a text editor that can write code and a script interpreter that explains execution.

Linux has a wide range of shells, commonly found in:

  • 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 the Bourne Shell, which is widely used in everyday work due to its ease of use and free use. Bash is also the default shell for most Linux systems.

In general, people don't distinguish between the Bourne Shell and the Bourne Again Shell, so, like the one that's!/bin/sh, it can also be changed to .!/bin/bash.

The program specified by the subsequent path to the system 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 a file), create a new file test.sh, extension sh (sh for shell), the extension does not affect script execution, see the name is good, if you write the shell script with php, the extension with php OK.

Enter some code, and the first line is usually like this:

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

Run an instance . . .

""!" is a convention tag that tells the system what interpreter the script needs to execute, which shell is used.

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 appropriate directory:

chmod +x ./test.sh  #使脚本具有执行权限
./test.sh  #执行脚本

Note that be sure to write as ./test.sh instead of test.sh, and run other binary programs as well, write test.sh directly, and the linux system goes to path to find out if there is no test.sh, and only /bin, /sbin, /usr/bin, /usr/sbin, etc. in PATH, your current directory is usually not in PATH, so write test.sh will not find the command, to tell the system with ./test.sh, just look for in the current directory.

2, as an interpreter parameter

This works by running the interpreter directly, with the parameters of the shell script's file name, such as:

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

Scripts that run this way do not need to specify interpreter information on the first line, and it is useless to write them.