Linux main shell command detailed (bottom)

Source: Internet
Author: User
Tags aliases terminates

command-line editing operations

Function

Ctrl+b or LEFT ARROW key

Move one character to the left (move to the previous character)

Ctrl+f or RIGHT ARROW key

Move one character to the right (move to the next character)

CTRL + A

Move to the beginning of the line

Ctrl+e

Move to end of line

ESC b

Move one word to the left

ESC F

Move right One word

Del

Delete the character at which the cursor is located

Ctrl+d

Delete the character at which the cursor is located

BACKSPACE or Ctrl+h

Delete the character to the left of the cursor

Ctrl+k

Delete to end of line

 

Command history

In bash, the history command is able to save recently executed commands. The history number of these commands starts at 1, and only a limited number of commands can be saved, up to 500, which is the historical record number of the history command with a default value of 500. To see the most recently executed command, simply type the history command and type Enter, the most recently executed commands are displayed in order (the number before each command is the historical number).

Cases

$ history

1 CP MyData today

2 VI MyData

3 MV MyData reports

4 CD Reports

5 ls

...

All these commands are called events, and an event indicates that an operation has occurred, that is, a command has been executed. These events are digitally identified according to the order in which they were executed, which is known as the historical event number. The event number of the last executed history event is the largest. Each event can be determined by its historical event number or the initial character or string of the command.

The history command enables you to query for previous events and display them to the command line to perform this event. The easiest way to do this is to use the UP and DOWN ARROW keys to show previous events to the command line. This operation does not need to run the history command to execute. Press the UP ARROW key, then the last occurrence of an event will appear on the command line, and then click Again, the previous event will appear on the command line; Pressing the DOWN ARROW key will cause the next event of the current event to appear on the command line.

Bash can also complete the character expansion of historical events by typing the ESC and tab keys. As with the standard command-line extension attributes, type a partial string of historical events, type ESC, and then type the TAB key, and historical events that match the string you just typed will automatically expand and echo back to the command line. If more than one event matches the input string, you will hear a ringing bell and continue typing the character or string, and the shell will uniquely determine the historical event that the user is typing.

There is also a command that queries and executes historical events ——! commands. After the! command, type the character associated with the history event, which can be the historical event number of the historical event or the first few characters of the event. In the following example, we query the event with a history event number of 3, then match it with a few characters at the beginning, and query the command.

Cases

$!3

MV MyData reports

$!mv

MV MyData reports

You can also query historical events with an offset (relative to the last event in the History event list). The negative offset is shifted forward from the end of the history event list. In the following example, the event "Vi MyData" with a historical event number of 2 is queried with a negative offset. It is important to note that this offset is relative to the last event in the History event list. In this case, the last event in the History Event list is event 5, and the first event in the History event list is 1. An event with a historical event number of 5, with a forward offset of 4, is an event with a historical event number of 2.

Cases

$!-4

VI MyData

If you type!!, the system defaults to the previous event. In the following example, the user types on the command line!! command, the system executes the previous event: "LS" command.

Cases

$ !!

Ls

MyData Today reports

You can also use "mode" to search for a historical event. The "mode" of the search must use the symbol "?" Enclosed. The following example uses "mode" "MyD?" To search for historical events "Vi MyData" with historical event number 3.

Cases

$ !? MyD?

VI MyData

1. Querying historical events

You can edit events in the History events list on the command line. Table 10-3 lists the various actions for the query history events list.

Table 10-3 Query History events Operations

Query History Event Action

Function

CTRL + N or down cursor key

Move to the next historical event of the current event in the History events list

Ctrl+p or up cursor key

Move to the previous historical event of the current event in the History events list

ESC <

Move to the top of the history events list

ESC >

Move to History Event list footer

!event_num

Use historical event numbers to locate a historical event

!characters

Querying a historical event with a character prefix for historical events

!? Pattern

Use "mode" to query events in the History events list

!-event_num

To locate historical events by offset
2. Configuring History:histfile and Histsize

The number of historical events saved by the system is stored in a specific system variable, which is histsize. The default value of this variable is usually set to 500. This value can be modified. For example:

$ histsize=10

Reset the value of Histsize to 10.

Historical events are saved in a file, and the file name is specified by the variable histfile. Usually the default name for this file is. bash_history. You can specify a new file name by assigning a value to the variable histfile.

Cases

$ echo $HISTFILE

/home/lisa/.bash_history

$ histfile= "/home/lisa/newhist"

$ echo $HISTFILE

/home/lisa/newhist

The above operation first displays the value of the variable histfile, then assigns it a new value of "/home/lisa/newhist", and all subsequent historical events will be saved in the Newhist file.

Alias

Another way to make work easier is to use a command alias. Command aliases are usually abbreviations for other commands to reduce keyboard input.

The command format is:

alias [alias-name= ' Original-command ']

Where Alias-name is the alias that the user gives to the command, Original-command is the original command and parameter. It is important to note that since Bash recognizes the original command with a space or carriage return, if you do not use quotation marks it may cause bash to intercept only the first word, resulting in an error. If no parameters are used after the alias command, the aliased command and its aliases that are currently in use are displayed. The alias that is taken for the command is always valid for the duration of the login. If the user needs an alias to be valid every time they log on, the alias command is written to the initialization script file.

If you frequently type the following command, it is best to create an alias for it to reduce the effort.

$ cd/usr/x11/lib/x11

If you create an alias named Goconfig for this long command, type the following command at the bash prompt:

$ alias goconfig= ' cd/usr/x11/lib/x11 '

Now, unless you exit bash, typing goconfig will have the same effect as the original long command. If you want to cancel the alias, you can use the following command:

$ Unalias Goconfig

This is an alias that many people consider useful, and can be written into the init script file to increase productivity:

Alias Ll= ' Ls–l '

Alias log= ' logout '

Alias ls= ' Ls–f '

If you are a DOS user and are accustomed to DOS commands, you can use the following alias definition to make Linux behave like DOS:

Alias dir= ' ls '

Alias copy= ' CP '

Alias Rename= ' MV '

Alias md= ' mkdir '

Alias rd= ' RmDir '

Note: When defining an alias, there can be no spaces on either side of the equals sign, or the shell cannot determine what you need to do. Quotation marks are required only if the command contains spaces or special characters.

If you type the alias command without any parameters, all defined aliases are displayed.

Prompt

Bash has a level two prompt. The first-level prompt is what bash often sees when waiting for a command to enter. The default value for the first-level prompt is the $ symbol. If the user does not like the symbol, or is willing to define the prompt himself, simply modify the value of the PS1 variable. For example, change it to:

ps1= "Enter a command:"

The second-level prompt is displayed when bash requires the user to enter more information in order to execute a command. Second level hint defaults think >. If you need to define the prompt yourself, simply change the value of the PS2 variable. For example, change it to:

ps2= "More information:"

The two examples above are those that set the prompt to a static string. In fact, users can also use some special characters that have already been defined beforehand. These special characters will cause the prompt to contain information such as the current time. Table 10-4 lists some of the most commonly used special characters and their meanings.

Table 10-4 Bash hint constant with special characters

 

Special characters

Description

!

Show the history number of the command

#

Shows the history number of the current command after the shell has been activated

$

Displays a $ symbol that displays the # symbol if the current user is root

\

Shows a backslash

D

Show current date

H

Displays the host name of the computer running the shell

N

Prints a newline character, which causes the prompt to cross the line

S

Displays the name of the shell that is running

T

Show Current Time

U

Displays the user name of the current user

W

Displays the current working directory base name

W

Show Current working directory

 

These special characters can be combined to provide users with a number of prompts to provide useful information. Here are a few practical examples:

ps1= "T"

The prompt will be made as follows:

02:16:15

and ps1=t

The prompt will be made as follows:

T

If ps1= "T\"

The prompt will be made as follows:

02:16:30

This example is obtained using a combination of two special characters.

Control how the shell runs

Bash has some special variables that can control the shell to work in different ways. For example, the variable noclobber prevents a file from being accidentally overwritten when the output is redirected. You can set the Noclobber variable to be valid or invalid through the SET command. The set command has two parameters: one is the option to specify the variable on or off (off), and one is the variable name of the special variable. To make a particular variable open (valid), with the-o option, to turn it off (invalid), use the +o option. For example:

$ set–o noclobber//Make Noclobber variable Open

$ set +o noclobber//Make noclobber variable off

The three most commonly used shell special variables are: ignoreeof, Noclobber, and Noglob.

Ignoreeof

The ignoreeof variable is used to prohibit the use of ctrl+d to exit the shell (ctrl+d not only exits the shell, but also terminates the user's input directly to the standard output. This operation is often used in some shell utility commands, such as the practical Command cat. In these utility operations, it is very easy to accidentally exit the shell by mistake. Ignoreeof Special variables are used to prevent such accidental exits. For example:

$ set–o ignoreeof

After that, the user can only exit the shell with the logout or Exit command.

Noclobber

The Noclobber variable can protect an existing file from being accidentally overwritten when the output is redirected. In the following example, the user setting Noclobber is valid, and when redirected, the user tries to overwrite the file myfile that already exists, and the system returns an error message.

Cases

$ set–o Noclobber

$ cat Preface>myfile

Bash:myfile:cannot Overwrite existing file

$

Noglob

After you set the NOGLOB variable, the shell does not extend some special characters or strings in the file name. such as the character * 、?、 [] will no longer be used as a wildcard character. If the user wants to list the file name answer that ends with?, you can use the following steps: First, the user makes the NOGLOB variable invalid, and then lists the file name. Can you see the question mark on the current command line? is considered to be a character in the file name and is no longer considered a wildcard.

$ set–o Noglob

$ ls answer?

Answer?

Child Shell and Export command

After the user logs on to the Linux system, a user shell is launched. In this shell, you can use a shell command or declare a variable, or you can create and run a shell script program. When you run a shell script, the system creates a child shell. At this point, there will be two shells in the system, one for the system-initiated shell at logon, and the other for the shell created by the system to run the script. When a script is finished running, its script shell terminates and can be returned to the shell before the script executes. In this sense, a user can have many shells, each of which is derived from a shell, called the parent shell.

A variable defined in a child shell is valid only within that child shell. If a variable is defined in a shell script, when the script is run, the defined variable is only a local variable within the script, and the other shell cannot reference it, so that the value of one variable can be changed in the other shell. You can use the Export command to output a defined variable. The export command will allow the system to define a copy of this variable when creating each new shell. This process is called variable output.

[Example] In this example, the variable myfile is defined in the Dispfile script program. The Export command is then used to output the variable myfile to any child shell, such as the child shell that is produced when the Printfile script is executed.

Dispfile Script Program Checklist:

Myfile= "List"

Export MyFile

echo "Displaying $myfile"

Pr–t–n $myfile

Printfile

 

Printfile Script Program Checklist:

echo "Printing $myfile"

LPR $myfile &

$dispfile

Displaying List

1 screen

2 Modem

3 paper

Printing List

$

Custom Bash

Many of the methods for customizing bash have been described in this section, but so far, these methods are only useful for the current Bash dialog. Any changes you make will be lost as long as the user exits the login. Therefore, you should make permanent changes in bash's initialization file.

The most common command that a user can put into an initialization file is the command to be executed each time bash is launched, which is the alias command and the variable definition. Each user in the system has a. bash_profile file in his or her home directory, and bash reads the file every time it starts, and all the commands it contains are executed.

Here is the code for the default. bash_profile file:

#.bash_profile

#Get the aliases and functions

If [-F ~/.BASHRC];then

. ~/.BASHRC

Fi

#User specific environment and startup programs

Path= $PATH: $HOME/bin

env= $HOME/.BASHRC

Username= ""

Export USERNAME ENV PATH

Linux main shell command detailed (bottom)

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.