Linux learning [16] bash learning in-depth 2---alias setting alias---history command---environment configuration related

Preface

Linux Learning 15 briefly mentions the alias command, which shows that it functions as an alias. This section will expand on it and write about it.
At the same time, the history instructions mentioned in the previous section are also recorded in this section.
Finally, there is the environment-related configuration. It is mainly because the environment configuration was modified when configuring the Raspberry Pi or other Linux systems. Some instructions were used. Here, a unified record will be made.


1. alias

alias directive, alias function.

If you want to query hidden files and need a long list and page-by-page browsing, you need to issue the "ls -al | more" command and simplify it through alias, which is a little easier.

For example:alias lm='ls -al | more'

File deletion commands under root permissions rmgenerally have very high permissions. We can rm -igenerate a reminder for secondary confirmation, but we cannot actually use the option to remind ourselves every time -i, and sometimes we may delete it by mistake.

In response to this situation, we can actually use the alias command to alias the original rm -icommand rm.
In this way, we rmwill conduct a second confirmation every time to prevent accidental deletion.
For example:alias rm='rm -i'

Cancel alias
unalias+别名的指令
In the two examples just now, we can cancel the alias unalias lmrespectively unalias rm.


2. history

We can press the up and down arrow keys on the console to select previously entered commands. This is where history has made a great contribution.
General usage:
history nList the latest n instructions
history -cand delete all history instructions of the current shell.
history -选项与参数

Options and parameters:
n: number, meaning "to list the latest n commands"
-c: eliminate all history content in the current shell
-a: add the current new history command In histfiles, if histfiles is not added,
~/.bash_history will be written by default.
-r: Read the contents of histfiles into the history memory of the current shell;
-w: Write the current history memory contents into histfiles.

Demo screenshots:
Commands: history 2
Displays the last two commands.
Insert image description here
Commands: history
List all entered commands. Only 20 commands are taken in the screenshot here.
Insert image description here


History writing problem.
When I used su to switch to the administrator before, I might open several console windows, all using root privileges to perform command operations.
For example, I enter command a in window A, command b in window B, and command c in window C.
So logically speaking, I entered the three commands abc as root. When using history, the three commands should be displayed. In fact, this is not the case, because historical commands will only be written when the console corresponding to the current user exits. In other words, when all three of my consoles are open, the last input of console A is the a command, but console A is not closed. When I press the history button on console B, I cannot see the command a just input by A. of.

For example, I opened three consoles with the root name. The first console checked the history commands and found that there were only five. Of course, the last one here includes the history just entered. The next three consoles were entered ls -al, ls -l, ls -aand I closed the last one.ls -a
Insert image description here

Insert image description here
There is an extra history here because I entered the history command twice.
After I close them all, the system will save the history instructions according to the order in which the console is closed.
Insert image description here


3. Environment configuration related

After we configure some startup parameters, they often do not take effect immediately. Some tutorials say that we need to restart the virtual machine or something.
For example, the following parameter will not take effect immediately after we modify it. It will take effect automatically after restarting.
Insert image description here

Restarting is a bit troublesome, so we can manually make it take effect immediately.

Instructions: source: Instructions for reading the environment configuration file.
You can use source or decimal point (.) to read the contents of the configuration file into the current shell environment! For example, if I modify ~/.bashrc, I don’t need to log out. I can immediately use source ~/.bashrc to read the latest settings into the current environment.

Summarize

This blog is a record of some small details in bash. I didn’t pay attention to aliases before reading the book. I only know how to switch commands by pressing the up and down keys. The environment configuration is probably what the tutorial writes and how I do it. After reading the book, I have a deeper understanding of these.

Supongo que te gusta

Origin blog.csdn.net/Edwinwzy/article/details/131265771
Recomendado
Clasificación