Simple understanding of linux shell

shell history:

The role of the Shell is to interpret and execute the user's command. When the user enters a command, the Shell interprets and executes one. This method is called Interactive. The Shell also has a method of executing commands called Batch. Write a Shell script (Script), which has many commands, and let the Shell execute these commands at one time instead of typing commands one by one. Shell scripts are very similar to programming languages. They also have variables and flow control statements, but shell scripts are interpreted and executed without compilation. The shell program reads and executes these commands line by line from the script, which is equivalent to a user putting the commands in the script. Type line by line to the Shell prompt to execute.

For historical reasons, there are many kinds of shells on UNIX systems:

  • 1.sh (Bourne Shell): Developed by Steve Bourne, various UNIX systems are equipped with sh.
  • 2.csh (C Shell): Developed by Bill Joy and released with BSD UNIX, its flow control statement is very similar to C language, and supports many functions that Bourne Shell does not support: job control, command history, command line editing.
  • 3.ksh (Korn Shell): Developed by David Korn, it is backward compatible with sh functions, and adds new functions introduced by csh. It is the standard configuration of many UNIX systems. On these systems, /bin/sh often points to A symbolic link to /bin/ksh.
  • 4. tcsh (TENEX C Shell): It is an enhanced version of csh, which introduces functions such as command completion, and replaces csh on FreeBSD, Mac OS X and other systems.
  • 5.bash (Bourne Again Shell): Shell developed by GNU, the main goal is to be consistent with the POSIX standard, while taking into account the compatibility of sh, bash borrows a lot of functions from csh and ksh, it is the standard configuration of various Linux distributions Shell, on Linux systems /bin/sh is often a symbolic link to /bin/bash. Even so, there are many differences between bash and sh. On the one hand, bash extends some commands and parameters. On the other hand, bash is not fully compatible with sh, and some behaviors are inconsistent, so bash needs to simulate the behavior of sh: when we When bash is started with the program name sh, bash can pretend to be sh, ignore extended commands, and behave the same as sh.
  • 6. The command completion function of zsh is very powerful, which can complete the path, complete the command, complete the parameter and so on.

Usually used .shand the most popular.bash

Check the shell type used by the user after logging in:

vim /etc/passwd

The last column shows the shell type corresponding to the user

write picture description here

After the user enters a command on the command line, the shell will generally fork a process and execute the command, but the built-in command of the shell is an exception. Executing the built-in command is equivalent to calling a function in the shell process, and does not create a new process. Commands such as cd, alias, umask, exit, etc. learned before are built-in commands. Any command that cannot find the location of the program file with the which command is a built-in command. There is no separate man manual for built-in commands. Looking at the built-in commands in the manual, it should be

man 内建命令

Such as export, shift, if, eval, [, for, while and so on. Although the built-in command does not create a new process, it will also have an Exit Status. Usually, 0 is used to indicate success and non-zero indicates failure. Although the built-in command does not create a new process, there will also be a status code after the execution ends. It can be read with the special variable $?.

echo $?

write picture description here

Execute the script:

example:

1. Write a simple script test.sh:

#! /bin/sh
cd ..
ls

Analysis: Comments are represented by # in Shell scripts, which are equivalent to // comments in C language. An exception is if # is at the beginning of the first line and is #! (called Shebang), which means that the script is interpreted and executed using the interpreter /bin/sh specified later.

2. Add executable permission to this script file and execute:

chmod a+x test.sh
./test.sh

The shell will fork a child process and call exec to execute ./test.shthe program. The exec system call should replace the code segment of the child process with the code segment of the ./test.sh program and start execution from its _start. However, test.sh is a text file, there is no code segment and _start function at all, what should I do? In fact, there is another mechanism for exec. If a text file is to be executed, and the interpreter is specified with Shebang on the first line, the current process is replaced with the code segment of the interpreter program, and the execution starts from the _start of the interpreter. , and this text file is passed to the interpreter as a command-line argument. Therefore, executing the above script is equivalent to executing a program

/bin/sh ./test.sh

Executing in this way does not require the test.sh file to have executable permissions.

If the commands entered on the command line are enclosed in () brackets, a subshell will also be forked to execute the commands in parentheses, and multiple commands separated by semicolons; can be entered in a line, such as:

(cd ..;ls -l)

The effect of executing the shell script with the above two methods is the same. The cd .. command changes the PWD of the subshell without affecting the interactive shell.

 cd ..;ls -l

There are different effects, the cd .. command is executed directly under the interactive shell, changing the PWD of the interactive shell

write picture description here

However, this approach is equivalent to executing a shell script like this:

source ./test.sh

or

. ./test.sh

sourceOr .the command is a built-in command of the shell, and this method will not create a sub-shell, but directly execute the command in the script line by line under the interactive shell.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325766551&siteId=291194637