Article title: Linux tutorial-alias. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Another way to make work easier is to use command aliases. The command alias is usually abbreviated to other commands to reduce keyboard input.
Command format:
Alias [alias-name = 'original-command']
Here, alias-name is the alias that the user gives the command, and original-command is the original command and parameter. Note that Bash recognizes the original command by space or carriage return. otherwise, Bash may intercept the first word and cause an error. If no parameters are used after the alias command, the alias command and its alias are displayed. The alias for the command is always valid during this logon. If you need an alias to be valid upon every login, write the alias command to the initialization script file.
[Example] if you often need to enter the following command, it is best to create an alias for it to reduce the workload.
$ Cd/usr/X11/lib/X11
If you create an alias named goconfig for this long command, enter 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. To cancel an alias, run the following command:
$ Unalias goconfig
These are some aliases that many people think are useful. they can be written into the initialization script file to improve work efficiency:
Alias ll = 'ls? L'
Alias log = 'logout'
Alias ls = 'ls? F'
If you are a DOS user and are used to the DOS command, you can use the alias below to make Linux behave like DOS:
Alias dir = 'Ls'
Alias copy = 'CP'
Alias rename = 'mmv'
Alias md = 'mkdir'
Alias rd = 'rmdir'
Note: When defining an alias, there cannot be spaces on both sides of the equal sign. otherwise, shell cannot decide what to do. Quotation marks are required only when the command contains spaces or special characters.
If you type an alias command without any parameters, all defined aliases are displayed.
Prompt
Bash has two levels of prompt. The first-level prompt is often seen when Bash is waiting for command input. The default value of the first-level prompt is the $ symbol. If you do not like this symbol or want to define a prompt, you only need to modify the value of the PS1 variable. For example, change it:
PS1 = "Enter a command :"
The second-level prompt is displayed when you need to enter more information for Bash to execute a command. The second-level prompt is> by default. If you need to define the prompt yourself, you only need to change the PS2 variable value. For example, change it:
PS2 = "More information :"
In the above two examples, the prompt is set to a static string. In fact, you can also use some special characters that have been defined in advance. These special characters will make the prompt contain information such as the current time. Table 10-4 lists some of the most common special characters and their meanings.
Table 10-4 common special characters in bash Prompt
These special characters can be combined to provide users with some prompts and useful information. Here are a few examples:
PS1 = "\ t"
The prompt will be changed to the following:
02:16:15
PS1 = \ t
The prompt will be changed to the following:
T
If PS1 = "\ t \\"
The prompt will be changed to the following:
02:16:30
This example is a combination of two special characters.
Control shell running mode
Bash has some special variables that can control shell to work in different ways. For example, the variable noclobber can prevent a file from being overwritten unexpectedly during the redirection output. The set command can be used to set whether the noclobber variable is valid or invalid. The set command has two parameters: one is to specify the on or off option of the variable, and the other is the variable name of the special variable. To enable (valid) a special variable, use the-o option to turn it off (invalid) and use the + o option. For example:
$ Set? O noclobber // enable the noclobber variable
$ Set + o noclobber // enable the noclobber variable
The three most common 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 is used not only to exit the shell, but also to terminate 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 shell by mistake. The ignoreeof special variable is used to prevent this unexpected exit. For example:
$ Set? O ignoreeof
You can only use the logout or exit command to exit the shell.
Noclobber
The noclobber variable can protect existing files when redirecting the output to prevent accidental overwrite. In the following example, the user sets noclobber as valid. during redirection, the user tries to overwrite the existing file myfile, and an error message is returned.
[Example]
$ Set? O noclobber
$ Cat preface> myfile
Bash: myfile: cannot overwrite existing file
$
Noglob
After the noglob variable is set, shell does not extend some special characters or strings in the file name. Such as character *,? And [] are no longer used as wildcards. If you want to end the list? Is the file name answer ?, You can perform the following steps: first, make the noglob variable invalid, and then list the file names. Can you see the question mark on the current command line? It is regarded as a character in the file name and is no longer considered as a wildcard.
$ Set? O noglob
$ Ls answer?
Answer?
Sub-shell and export commands
After a user logs on to Linux, the system starts a user shell. In this shell, you can use shell commands or declare variables, or create and run shell scripts. When you run the shell script program, the system creates a sub-shell. At this point, there will be two shells in the system, one is the shell started by the system at login, and the other is the shell created by the system for running the script program. When a script program runs successfully, its script shell is terminated and can be returned to the shell before the script is executed. In this sense, you can have many shells, each of which is derived from a shell (called the parent shell.
The variables defined in the sub-shell are valid only in the sub-shell. If a variable is defined in a shell script program, when the script is running, the defined variable is only a local variable in the script program, and other shells cannot reference it, to change the value of a variable in other shells, you can use the export command to output the defined variables. The export command enables the system to define a copy of this variable when creating a new shell. This process is called variable output.
[Example] in this example, the variable myfile is defined in the dispfile script. Use the export command to output the variable myfile to any sub-shell, for example, the sub-shell generated when the printfile script program is executed.
List of dispfile script programs:
/************** Begin dispfile **************/
Myfile = "List"
Export myfile
Echo "Displaying $ myfile"
Pr? T? N $ myfile
Printfile
/************* End dispfile ***************/
Printfile script program list:
/************** Begin printfile **************/
Echo "Printing $ myfile"
Lpr $ myfile &
/************* End printfile **************/
$ Dispfile
Displaying List
1 screen
2 modem
3 paper
Printing List
$
Custom Bash
Many methods for customizing Bash have been introduced in this section, but so far, these methods are only useful for the current Bash conversation. As long as the user logs out, all changes will be lost. Therefore, permanent changes should be made in the Bash initialization file.
You can put the commands required to start Bash into the initialization file. The most common commands are alias commands and variable definitions. Each user in the system has a. bash_profile file in the main directory. Bash reads the file every time it starts, and all the commands contained in the file will be executed.
The code for the default. bash_profile file is as follows:
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.