Shell introduction, history commands, command completion and aliases, wildcard characters, input and output redirection

Source: Internet
Author: User
Tags aliases

Shell Introduction

The shell is the intermediate medium used when the system interacts with the computer hardware, and it is just a tool of the system. In fact, there is a layer of stuff between the shell and the computer hardware-the system kernel.

The user directly faces not the computer hardware but the shell, the user instructs the shell, then the shell again to the system kernel, and then the kernel to control the computer hardware to perform various operations.

The shell version installed by default for Redhat and Centos is bash, which is an enhanced version of SH.



Historical command

The commands we have executed Linux are recorded, and presets can record 1000 historical commands. These commands are saved in the. bash_history file in the user's home directory. However, a command that runs in the current shell is saved to the. bash_history file only if the user exits the shell normally (exit the terminal normally using commands exit or logout, and directly close the remote interface is not a normal exit).

The root user's command is guaranteed to exist:/root/.bash_history

Normal user's command is persisted:/home/user name/.bash_history


You can view history commands through Cat/root/.bash_history, or you can view historical commands with the history command. It is important to note, however, that history's view of historical commands may be different from the historical commands that cat/root/.bash_history view, because the command stored in the history command is temporarily present in memory and only when you exit the shell normally. Commands that run in the current shell are saved in the./bash_history.


Echo $HISTSIZE can see how many history commands are currently saved

650) this.width=650; "src=" Https://s3.51cto.com/oss/201711/15/74b3288f16625c5993f3829b26f54245.png "title=" Clipboard (5). png "alt=" 74b3288f16625c5993f3829b26f54245.png "/>

History-c empties the history command in memory. However, the historical commands in the./root/.bash_history still exist.


You can modify the number of commands saved by modifying the HISTSIZE environment variable.

Modify the Histsize parameter in the/etc/profile file to modify the number of commands saved.

650) this.width=650; "src=" Https://s2.51cto.com/oss/201711/15/fa46e654f37e59e5f7387dfb20f761f1.png "title=" Clipboard (1). png "alt=" Fa46e654f37e59e5f7387dfb20f761f1.png "/>

After fixing the number of the histsize, you can modify the number of bars saved by the command.

Change the number of Save history commands to 5000, save and exit, perform echo $HISTSIZE find the number of historical commands saved or 1000. This requires restarting the terminal, or executing the command source/root/profile to take effect.

650) this.width=650; "src=" Https://s4.51cto.com/oss/201711/15/166eca564e92fb08e39b2df84f1845dd.png "title=" Clipboard. png "alt=" 166eca564e92fb08e39b2df84f1845dd.png "/>


Record the run time of a command

Execute command: histtimeformat= "%y/%m/%d%h:%m:%s"

After execution, it only takes effect temporarily, if it is replaced by a terminal or after restarting the terminal.

To be permanently active, you need to place the histtimeformat= "%y/%m/%d%h:%m:%s" command in the/etc/profile file.

650) this.width=650; "src=" Https://s3.51cto.com/oss/201711/15/50ce87bd34f12a2720f5872a8af0e3bc.png "title=" Clipboard (3). png "alt=" 50ce87bd34f12a2720f5872a8af0e3bc.png "/>

When you view the history command, the time to use the command is displayed.

650) this.width=650; "src=" Https://s4.51cto.com/oss/201711/15/5134a4de38ab0c4de0d750a2cb410011.png "title=" Clipboard (9). png "alt=" 5134a4de38ab0c4de0d750a2cb410011.png "/>


Permanently save the input command history

Add hidden permissions to the file a (append only, cannot delete the modification.) )

Chattr +a/root/.bash_history



!! : Two consecutive exclamation marks indicate the execution of the previous instruction.

650) this.width=650; "src=" Https://s3.51cto.com/oss/201711/15/7d87d88152c3c6109a82cf284b9f8fd0.png "title=" Clipboard. png "alt=" 7d87d88152c3c6109a82cf284b9f8fd0.png "/>


! The N: N represents a number that represents the nth instruction that executes the command history.

650) this.width=650; "src=" Https://s1.51cto.com/oss/201711/15/1a1b56833e0116172c94247813dc7d53.png "title=" Clipboard (7). png "alt=" 1a1b56833e0116172c94247813dc7d53.png "/>


! String (string greater than or equal to 1):! PW executes the last command in the command history that begins with PW:

650) this.width=650; "src=" Https://s2.51cto.com/oss/201711/15/6a90071fbe8bc97a7270749ada9c21a9.png "title=" Clipboard (2). png "alt=" 6a90071fbe8bc97a7270749ada9c21a9.png "/>


Command completion

The TAB key can be used to complete commands, paths, and file names. Press the TAB key consecutively and the system will list all commands or files.


Only command completion is currently supported, and parameters are not supported for completion. For example: systemctl restart Network.service command, the first system can be tab-complete, but after the restart Network.service is not complete. Need to install package Yum install-y bash_completion. Takes effect after a restart.



Aliases Alias

Use alias to name a common and long command as a simple and easy-to-remember instruction. Unalias can de-alias functionality.

Just enter alias to see which commands are aliased.

650) this.width=650; "src=" Https://s1.51cto.com/oss/201711/15/27752e51400db1325969154b5cd9d2aa.png "title=" Clipboard.png "alt=" 27752e51400db1325969154b5cd9d2aa.png "/>


Alias aliases = ' specific commands '. Temporary effect.

650) this.width=650; "src=" Https://s2.51cto.com/oss/201711/15/e6679c9433ad5779c6cb096cf65c3980.png "title=" Clipboard (6). png "alt=" E6679c9433ad5779c6cb096cf65c3980.png "/>


Several alias settings are configured under the. bashrc file under User files. There are also some scripts that are defined in the/etc/profile.d/directory.


Remove Alias Unalias

650) this.width=650; "src=" Https://s4.51cto.com/oss/201711/15/c146518807f767771a3227df81bd4701.png "title=" Clipboard (4). png "alt=" C146518807f767771a3227df81bd4701.png "/>


Wildcard characters

Under Bash, you can use * to match 0 or more characters, with a. Matches one character.

650) this.width=650; "src=" Https://s3.51cto.com/oss/201711/15/e62b10e85aa07d3f147c3ed5cfcfcf3e.png "title=" Clipboard (8). png "alt=" E62b10e85aa07d3f147c3ed5cfcfcf3e.png "/>


Input and output redirection

Input redirection is used to change the input of the command, and output redirection is used to change the output of the command. Output redirection is often used to enter the results of a command into a file, rather than on the screen. The command to enter the redirect is <, and the output redirection command is >. The command to redirect the error is 2>, and the command to chase the weighted direction is >>.

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= " Https://s5.51cto.com/oss/201711/15/479febe7a96f6b4ccf09057b03d7151d.png "title=" clipboard (one). png "alt=" 479febe7a96f6b4ccf09057b03d7151d.png "/>


This article is from the "Network Management Learning Road" blog, please be sure to keep this source http://754599082.blog.51cto.com/11510506/1982024

Shell introduction, history commands, command completion and aliases, wildcard characters, input and output redirection

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.