Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to make Bash Command History more useful in linux

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to make the history of Bash commands more useful in linux, I believe most people don't know much about it, so share this article for your reference. I hope you can learn a lot after reading this article. Let's learn about it together.

Tell Bash what you want it to remember, or even delete unwanted records and rewrite history.

The Linux terminal running Bash has a built-in history that you can use to track recent operations. To view the history of your Bash session, use the built-in command history:

$echo "foo" foo $echo "bar" bar $history 1 echo "foo" 2 echo "bar" 3 history

Unlike most commands, the history command is not an executable on the file system, but a function of Bash. You can use the type command to verify:

$type history history is a shell builtin history control

The upper limit of rows for shell history is defined by the HISTSIZE variable. You can set this variable in the .bashrc file. Set your history to 3000 lines, and then delete the earliest lines to make room for the latest command, which is at the bottom of the list:

Export HISTSIZE=3000

There are other history-related variables. The HISTCONTROL variable controls which histories are recorded. You can force Bash to exclude commands that start with a space by writing the following line in .bashrc:

Export HISTCONTROL=$HISTCONTROL:ignorespace

Now, if you enter a command that starts with a space, it will not be recorded in the history:

$echo "hello" $mysql-u bogus-h badpassword123 mydatabase $echo "world" $history 1 echo "hello" 2 echo "world" 3 history

You can also avoid duplicate entries:

Export HISTCONTROL=$HISTCONTROL:ignoredups

Now, if you enter two commands one by one, only one will appear in the history:

$ls $ls $ls $history 1 ls 2 history

If you like these two ignore features, you can use ignoreboth:

Export HISTCONTROL=$HISTCONTROL:ignoreboth removes commands from history

Sometimes you make a mistake by typing something sensitive in shell, or you just want to clean up your history so that it more accurately represents the steps taken to make something work. If you want to delete a command from the history of Bash, use the-d option on the line number of the item you want to delete:

$echo "foo" foo $echo "bar" bar $history | tail 535 echo "foo" 536 echo "bar" 537 history | tail $history-d 536$ history | tail 535 echo "foo" 536 history | tail 537 history-d 536 538 history | tail

To stop adding history entries, as long as you have ignorespace in the HISTCONTROL environment variable, you can add spaces before the command:

$history | tail 535 echo "foo" 536 echo "bar" $history-d 536$ history | tail 535 echo "foo"

You can use the-c option to clear all session history:

Lessons learned from the $history-c $history $history command

Manipulating history is usually not as dangerous as it sounds, especially if you manage it purposefully. For example, if you want to record a complex problem, it is usually best to use session history to record commands, because by inserting commands into the history, you can run them and test the process. In many cases, failure to execute history commands can lead to ignoring small steps or miswriting small details.

Use historical sessions on demand and control history wisely.

These are all the contents of the article "how to make the history of Bash commands more useful in linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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: 242

*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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report