Usage of the alias command

Source: Internet
Author: User
Tags aliases

Aliases alias alias

The Linux system assigns aliases to commands under the alias alias command usage:
In a Linux system, if the command is too long and does not conform to the user's habits, then we can assign an alias to it. Although it is possible to establish a link for a command to solve a long file name problem, the link is powerless for commands with command-line arguments. Specifying an alias can solve all of these problems. Just give me some examples:
Alias l= ' ls-l '; replace Ls-l command with L (Xenix a similar l command)
Alias CD ... = ' CD ... ' with CD. instead of CDs. Command (used for CD in DOS). People help a lot.
Alias md= ' mkdir '; use MD instead of mkdir command (pair in DOS ...)
Alias c:= ' mount/dev/hda1/mnt/c & cd/mnt/c '; Replace command sequence with C: command: Install DOS partition, then enter.
Usually we can put the above commands in the. bash_prifle file in our home directory and use the source. Bash_profile command.

===========================================


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 the command, Original-command is the original command and parameter. Note that because bash identifies the original command with a space or a carriage return, the failure to use quotes can cause bash to intercept only the first word, resulting in an error. If no arguments are used after the alias command, the alias command and its alias that are currently in use are displayed. The alias for the command is always valid during the logon period. If the user needs an alias to be valid at every logon, the alias command is written to the initialization script file.
[Example] if you frequently want to type the following command, it is a good idea 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, 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 previous long command. If you want to remove the alias, you can use the following command:
$ unalias goconfig
This is an alias that many people think is useful and can be written to the initialization script file to improve 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 you define an alias, you cannot have spaces on either side of the equal sign. Otherwise the shell cannot decide what you need to do. Quotation marks are required only if the command contains spaces or special characters. If you type an alias command with no arguments, all the defined aliases are displayed.

prompt
Bash has a level two prompt. The first-level prompts are frequently seen when bash is waiting for the command to enter. The default value for the first-level prompt is the $ symbol. If the user does not like this symbol, or if they prefer to define a prompt themselves, 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 users to enter more information for the execution of a command. The second level prompts Fummer to 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 scenarios where the prompt is a static string. In fact, users can also use some of the previously defined special characters. These special characters will cause information such as the current time to be included in the prompt . Table 10-4 lists some of the most common special characters and their meanings.


Table 10-4 Bash prompts constants with special characters

Special characters
Description
!
Displays the history number of the command
#
Displays the history number of the current command after Shell activation
$
Displays a $ symbol and displays the # symbol if the current user is root
/
Display a backslash
D
Show current date
h
Displays the computer host name on which the shell is running
N
Print 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 datum name
W
Show Current working directory
These special characters can be combined to provide users with a number of prompts that provide useful information. Here are a few practical examples:

ps1= "T"
Will make the prompt as follows:
02:16:15
and ps1=t
Will make the prompt as follows:
T
If ps1= "t/"
Will make the prompt as follows:
02:16:30
This example is obtained using a combination of two special characters.

control how the shell works


Bash has some special variables that can control the shell to work in different ways. For example, variable noclobber can prevent accidental overwriting of a file while redirecting output. The SET command allows you to set the Noclobber variable to be valid or invalid. The set command has two parameters: one is the option to specify the variable on or off, and the variable name of the special variable . To make a particular variable open (valid), use the-o option to turn it off (invalid) with 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 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 utility Command cat. In these utility operations, it is very easy to mistakenly operate and unexpectedly exit the shell. Ignoreeof Special variables are used to prevent this unexpected exit. For example:
$ set–o ignoreeof
After that, the user can only exit the shell with the logout or Exit command.


Noclobber
The noclobber variable protects files that already exist when redirecting output, preventing accidental overwriting. in the following example, the user set Noclobber to be valid, and when redirected, the user attempts to overwrite the existing file myfile, 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 a wildcard character. If the user wants to list the file name answer that ends with?, you can use the following procedure: First, the user makes the NOGLOB variable invalid, and then lists the file name. As you can see, the question mark on the command line now is considered a character in the filename and is no longer considered a wildcard.
$ set–o Noglob
$ ls answer?
Answer?

Sub-shell and Export command


After the user logs on to the Linux system, a user shell is started. In this shell, you can use shell commands or declare variables, or you can create and run shell script programs.when you run the shell script program, a child shell is created. 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, its script shell terminates and can be returned to the shell before executing the script.In this sense, users can have many shells, each of which is derived from a shell called the parent shell.
Variables defined in a child shell are 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 a variable can be changed in the other shell. You can use the Export command to export 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 case, the variable myfile is defined in the Dispfile script. The Export command is then used to output the variable myfile to any child shell, such as the child shell that was generated when the Printfile script was executed.
Dispfile List of script programs:
/**************begin dispfile**************/
Myfile= "List"
Export MyFile
echo "Displaying $myfile"
Pr–t–n $myfile
Printfile
/**************end dispfile***************/
 
Printfile List of script programs:
/**************begin printfile**************/
echo "Printing $myfile"
LPR $myfile &
/**************end printfile**************/
Run as follows:

$dispfile
displaying List
1 screen
2 Modem
3 paper
Printing List
$

Custom Bash


Many of the methods for customizing bash are covered in this section, but so far these methods are only useful for current bash dialogs. Any changes you make will be lost as long as the user logs out. So you should make a permanent change in the initialization file for bash. The most common command for users to put the commands required to start bash into the initialization file is the alias command and the variable definition. Each user in the system has a. bash_profile file in their home directory, and bash reads the file every time it starts, and all of the commands that it contains are executed.

The following is the code for the default. bash_profile file:
#.bash_profile
#Get the aliases and functions
if [-F ~/.BASHRC];then
. ~/.ba SHRC
Fi
#User specific environment and startup programs
Path= $PATH: $HOME/bin
env= $HOME/.BASHRC
Username= ""
Export USERNAME ENV PATH

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.