In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the example analysis of Linux Bash aliases, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
How many times have you typed a long command multiple times on the command line and want a way to save it later? This is where the Bash alias comes in handy. They allow you to condense long and mysterious commands into commands that are easy to remember and use.
Image-20211201131023527 checks Bash aliases in Linux
Bash Alias is a shell built-in command that can be confirmed by running the following command:
$type-an aliasalias is a shell builtin
Before jumping and setting aliases, you first need to see the configuration file involved. An alias can be set at the user level or the system level.
If you call the system shell, simply type "alias" in the terminal to see a list of defined aliases.
$alias
(check the list of defined Linux aliases)
Define the user-level name in the * * .bashrc file or .bash _ aliases file. The. Bash_aliases file is to put all aliases into a separate file, rather than putting it in the .bashrc file and other parameters. At first,. Bash_aliases** is not available, so you must create it.
$ls-la ~ | grep-I. bash _ aliases # check whether the file can be created with $touch ~ / .bash_aliases # to create an empty alias file
(create Bash alias file)
Open the * * .bashrc file and note the following section. The function of this code is to check whether the file .bash _ aliases,** exists in the user's home directory and load it each time you start a new terminal session.
# Alias definitions.# You may want to put all your additions into a separate file like# ~ / .bash_aliases, instead of adding them here directly.# See / usr/share/doc/bash-doc/examples in the bash-doc package.if [- f ~ / .bash_aliases]; then. ~ / .bash_aliasesfi
You can also create a custom alias file in any directory and add a definition in * * .bashrc or .profile to load it. But I don't like it. Personally, I prefer to group all aliases under .bash _ aliases**.
Of course, you can also add aliases under the * * .bashrc file. Look for the aliases section under the .bashrc * * file with some predefined aliases.
# enable color support of ls and also add handy aliasesif [- x / usr/bin/dircolors]; then test-r ~ / .dircolors & & eval "$(dircolors-b ~ / .dircow)" | | eval "$(dircolors-b)" alias ls='ls-- color=auto' # alias dir='dir-- color=auto' # alias vdir='vdir-- color=auto' alias grep='grep-- color=auto' alias fgrep='fgrep-- color=auto' alias egrep='egrep-- color=auto'fi# colored GCC warnings and errors#export GCC_COLORS='error=01 31 some more ls aliasesalias ll='ls-alF'alias la='ls-A'alias l='ls-CF'# Add an "alert" alias for long running commands. Use like so:# sleep 10; alertalias alert='notify-send-- urgency=low-I "$([$? = 0] & & echo terminal | | echo error)"$(history | tail-N1 | sed-e'\'s / ^\ s * [0-9]\ +\ sdebunkard / [; & |]\ swarning alertUniverse'')" create aliases in the Linux system
You can create temporary aliases that will be stored only for the current session and will be destroyed at the end of the current session, or you can create permanent aliases that will be persistent.
Command to create an alias in Linux:
$alias = "command to run"
For example, create the following aliases.
$alias Hello= "echo welcome to idccoupon"
Open the terminal and create any alias commands you need. Note: if you open another session, the newly created alias will not be available.
$alias Hello "echo welcome to idccoupon" $alias$ Hello
To make the alias permanent, add it to the *. Bash_aliases** file. You can use your favorite text editor, or you can add aliases using the cat command or the echo command.
$echo alias nf= "neofetch" > > ~ / .bash_aliases$ cat > > ~ / .bash_aliases$ cat ~ / .bash_aliases
(list aliases defined)
If you want it to work, you must reload the *. Bash_aliases** file for the changes to take effect in the current session.
$source~/ .bash _ aliases
Now, if I run * * "nf" (which is an alias for "neofetch"), it will trigger the neofetch** program.
$nf
(run Alias in Linux)
In addition, an alias can override any command you want. For demonstration purposes, a uptime command will be used here, which will show the system uptime, the number of users logged in, and the average system load. Now I'll create an alias that will override the behavior of the uptime command.
$uptime$ cat > > ~ / .bash_aliases alias uptime= "echo'I am running uptime command now'" $source ~ / .bash_aliases$ uptime
(override the default behavior of the command)
It can be concluded from this example that the priority belongs to the bash alias before checking and invoking the actual command.
$cat ~ / .bash_aliases$ source ~ / .bash_aliases$ uptime deletes aliases in Linux
Now, remove the uptime entry from the * *. Bash_aliases file, and then reload the .bash _ aliases file, which will still define the output uptime with an alias. This is because the alias definition is loaded into the current Shell session, so you must start a new session or cancel the alias definition by running the unalias** command, as shown in the following figure.
$unalias uptime
(delete aliases in Linux)
Note: Unalias removes the alias definition from the currently loaded session, not from * * .bashrc or .bash _ aliases.
Add a system-wide alias
At this point, we have learned how to set aliases at the user level. If you want to set aliases globally, you can modify the * * "/ etc/bash.bashrc" file and add aliases that will take effect globally. Of course, you need to have elevated privileges to modify bash.bashrc** files.
Alternatively, create a script under * * "/ etc/profile.d/". When logging in to shell, "/ etc / profile" will run any script under profile.d, and then actually run ~ / .profile * *. This approach reduces the risk of messing up * * / etc/ profile or / etc/bash.bashrc** files.
$sudo cat > > / etc/profile.d/alias.shalias ls= "ls-ltra"
Here is the code obtained from * * / etc/profile, which is responsible for running all the scripts we put under / etc/profiles.d/**. It looks for all files with the .sh extension and runs the source command.
$tail / etc / profile
Note: prior to actual operation, the best practice is to back up user-level or system-level files. In case something goes wrong, you can restore the backup copy
Thank you for reading this article carefully. I hope the article "sample Analysis of Linux Bash aliases" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.