Understanding the Shell (Chapter fifth)

Source: Internet
Author: User
Tags builtin syslog

1. Type of shell
[Email protected] ~]# ls-l/bin/*sh
-rwxr-xr-x. 1 root root 938832 Jul 2013/bin/bash
lrwxrwxrwx. 1 root root 4 Oct 2014/bin/csh-tcsh
-rwxr-xr-x. 1 root root 109672 Oct 2012/bin/dash
lrwxrwxrwx. 1 root root 4 Oct 2014/bin/sh-bash
-rwxr-xr-x. 1 root root 387328 Feb 2013/bin/tcsh
Running the above name in CENTOS6 can be seen in the CentOS shell with bash, csh, Dash. The default startup of the terminal in CentOS is generally bash.
Entering a bash command in command-line mode generates a child shell that exits the child shell using the Exit command

2. The shell's parent-child relationship
The shell process is a parent-child relationship, the parent shell can create a child shell through the Bash command, the child shell can also be created by the Bash command to re-create the child shell, through the ps-f command can see the relationship between the parent-child shell process, through the PS--forest You can view the hierarchical relationships between parent and child shells. When you run a shell command, SEHLL creates a child to perform the change command

3. List of processes
When we want to run more than one command at a time, we can add (;) between commands and commands so that the shell executes the commands sequentially. As follows:
[Email protected] ~]# pwd;ls;cd/etc
/root
Anaconda-ks.cfg Documents install.log.syslog Pictures Templates
Desktop Downloads Music Public Videos
[Email protected] etc]#

But the above is not a list of processes, add the above command to a (), so that the shell will only generate a child shell to execute these commands, as follows:
[Email protected] ~]# (PWD;LS;CD/ETC;PWD)
/root
Anaconda-ks.cfg Documents install.log.syslog Pictures Templates
Desktop Downloads Music Public Videos
/etc

There is also a way to create a list of processes, it does not create a child shell process, the command is placed in {}, the {} End with a space separated, the command must be added (;), as follows:
[[email protected] ~]# {pwd;ls;cd/etc;pwd;}
/root
Anaconda-ks.cfg Documents install.log.syslog Pictures Templates
Desktop Downloads Music Public Videos
/etc

To know whether a child process has been generated can be viewed using the value of the $bash_subshell environment variable, 0 indicates that no child process was generated, and 1 indicates that a child process was generated
[Email protected] ~]# (Pwd;ls;cd/etc;echo $BASH _subshell)
/root
Anaconda-ks.cfg Documents install.log.syslog Pictures Templates
Desktop Downloads Music Public Videos
1

[[email protected] ~]# {Pwd;ls;cd/etc;echo $BASH _subshell;}
/root
Anaconda-ks.cfg Documents install.log.syslog Pictures Templates
Desktop Downloads Music Public Videos
0

The overhead of using a child process is large because the terminal controls the I/O of the child process

4. Background mode
A process that starts with a terminal is typically run in the foreground and can be cut to the background in the following ways
COMMADN &: After command in &, you can run the command to the background
CTRL + Z: do this when the command runs to go to the background to run the command

Although the above method is sent to the background to run but still related to the terminal, if you need to peel off the connection with the terminal, you should use the following methods:
Nohub COMMAND &

In the daily use of the background mode can be used, so that command execution is not subject to terminal I/O, improve efficiency, such as file archiving:
$ (tar-cf Rich.tar/home/rich; TAR-CF my.tar/home/christine) &
[3] 2423

The jobs command can be used to view the processes currently running in the background, or jobs-l to see more detailed information, and once the task is complete, you will see the done prompt
You can also control jobs in the following ways
FG [[%] job_num]: The designated background work is recalled to the foreground;
BG [[%] job_num]: Send back to the background of the job continue to run;
Kill [[%] job_num]: Terminates the specified job

You can use an interesting command to view background jobs:
Sleep # &: Let process sleeps # seconds, at which point you can use the Jobs command to view background jobs

The Coproc command, which allows the command to run in the background, creates a child shell and then runs the specified command in the child shell, for example:
[[email protected] etc]# Coproc {sleep 10;}
[1] 24343
[Email protected] etc]# jobs-l
[1]+ 24343 Running coproc coproc {sleep ten;} &

5. External commands and built-in commands
You can differentiate between external commands and built-in commands by using the type command:
[[Email protected] etc]# type cd
CD is a shell builtin
[Email protected] etc]# type Htop
Htop Is/usr/bin/htop
As can be seen from the CD is a built-in command, Htop is an external command, you can see more detailed information through TYPE-A, through the which command to see the path of the command.

Some commands exist both externally and in-built, such as PWD:
[Email protected] etc]# type-a pwd
PWD is a shell builtin
PWD is/bin/pwd
[email protected] etc]# which PWD
/bin/pwd

Built-in commands do not create child processes to run at run time, and are more efficient than external commands.

Common built-in commands are:
History: Used to manage command histories, when logging in to the shell, the record in the command history file is read, ~/.bash_history, and the command that executes after the login shell is appended to the command history file when the shell exits.
Parameters:
-A: Appends commands executed in the current shell to ~/.bash_history
-D #: Remove commands from the specified numbered command history
-C: Empty command history
Quick action:
!! : Call the previous command
!#: Invoking the command history in the # command
! String: Invokes the most recent command in a command history that starts with a string

Alias/unalias: Command alias Management
Alias alias = command name (if you include a space in the command name plus quotation marks): Create an alias
Unalias aliases
Use the command to modify only the current shell is valid, if you want to be permanently small, you need to modify the ~/.BASHRC file.

This article from "The Boundless" blog, declined reprint!

Understanding the Shell (Chapter fifth)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.