Shell Introduction
Although not part of the Unix/Linux kernel,But it
It calls most of the core functions of the system to execute programs, create files, and coordinate the running of various programs in parallel.. Therefore, shell is the most important utility for users. It is the key to making good use of Unix/Linux systems by thoroughly understanding and mastering shell features. It can be said that,
Shell proficiency reflects the user's proficiency in Unix/Linux.
Shell has two methods to execute commands:
- Interactive): Explain the execution of a user's command. If the user enters a command, shell will explain the execution.
- Batch Processing (Batch): The user writes a shell script in advance, which contains many commands, so that the shell can finish executing these commands at a time without having to knock the command one by one.
Shell scripts are similar to programming languages, and there are also variables and process control statements. However, shell scripts are interpreted and executed without compilation, the shell program reads and executes these commands from one line in the script, which is equivalent to a user hitting the shell prompt to execute the command line in the script.
Several common Shells
Shell is a scripting language, so there must be an interpreter to execute these scripts.
Common shell script interpreters in Unix/Linux include bash, sh, CSH, and KSh. They are used to being called a shell.
We often talk about the number of shell types. In fact, it refers to the shell script interpreter.
- Bash:Bash is the default shell for Linux.Bash is jointly completed by brianfox and chetramey. It is short for the bourneagain shell and has a total of 40 internal commands.
- Sh: Sh is developed by Steve Bourne and is short for bourneshell,SH is the default shell for Unix.
- Ash: Ash shell is compiled by kenth almqualified. It is a small shell that occupies the least system resources in Linux. It contains only 24 internal commands, so it is inconvenient to use.
- CSH:CSH is a large Linux kernel.It is compiled by a total of 47 authors represented by William joy and contains 52 Internal commands.This shell actually points to a shell like/bin/tcsh, that is, CSH is actually tcsh.
- Ksh: Ksh is short for the Korn shell. It is written by Eric Gisin and contains 42 Internal commands. The biggest advantage of this shell is that it is almost completely compatible with the commercial release ksh, so that you can try the performance of the commercial version without spending money to buy the commercial version.
Note:
Bash is the abbreviation of the Bourne again shell, which is the default shell of the Linux standard. It is based on the bourneshell,Some features of C shell and Korn shell are absorbed.
Bash is fully compatible with SH. That is to say, scripts written with SH can be executed in bash without modification.
Shell itself is a program written in C language. It serves as a bridge for users to use Unix/Linux, and most of their work is done through shell.Shell is both a command language and a programming language.. As a command language,It interactively interprets and executes user-input commandsAs a programming language,It defines various variables and parameters and provides many control structures that are available in advanced languages, including loops and branches..
First shell script
Open the text editor and create a new file,If the extension is SH (SH indicates shell), the extension does not affect script execution.If you use PHP to write shell scripts, you can use PHP for the extension. Enter some code:
#!/bin/bashecho "Hello World !"
"#!" Is an agreed tag, which tells the system what interpreter is required to execute this script, that is, which shell is used. The echo command is used to output text to the window.
There are two methods to run shell scripts.
- 1 As an executable program
# Save the above Code as test. SH and CD it to the corresponding directory: chmod + X./test. Sh # grant the script execution permission./test. Sh # execute the script
Note,
Be sure to write it as./test. Sh instead of test. Sh. The same applies to other binary programs., Directly write test. sh, the Linux system will go to the path to find whether it is named test. sh, and only/bin,/sbin,/usr/bin,/usr/sbin and so on in the path. Your current directory is usually not in the path, so it is written as test. sh cannot find the command. /test. sh tells the system to find it in the current directory.
Run the bash script in this way,
The first line must be correct so that the system can find the correct interpreter.The "system" here is actually a shell application, but I deliberately write it into a system for ease of understanding. Since this system refers to shell,
Can a script using/bin/sh as the interpreter Save the first line? Yes.
- 2 as interpreter Parameter
This operation mode is,
Run the interpreter directly. The parameter is the file name of the shell script.Such:
/bin/sh test.sh/bin/php test.php
Scripts run in this way,
You do not need to specify the interpreter information in the first line. It is useless to write the interpreter.
Shell tutorial