38

Better Bash History (2012)

 5 years ago
source link: https://www.tuicool.com/articles/qQzyUfM
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

By default, the Bash shell keeps the history of your most recent session in the .bash_history file, and the commands you’ve issued in your current session are also available with a history call. These defaults are useful for keeping track of what you’ve been up to in the shell on any given machine, but with disks much larger and faster than they were when Bash was designed, a little tweaking in your .bashrc file can record history more permanently, consistently, and usefully.

Append history instead of rewriting it

You should start by setting the histappend option, which will mean that when you close a session, your history will be appended to the .bash_history file rather than overwriting what’s in there.

shopt -s histappend

Allow a larger history file

The default maximum number of commands saved into the .bash_history file is a rather meager 500. If you want to keep history further back than a few weeks or so, you may as well bump this up by explicitly setting $HISTSIZE to a much larger number in your .bashrc . We can do the same thing with the $HISTFILESIZE variable.

HISTFILESIZE=1000000
HISTSIZE=1000000

The man page for Bash says that HISTFILESIZE can be unset to stop truncation entirely, but unfortunately this doesn’t work in .bashrc files due to the order in which variables are set; it’s therefore more straightforward to simply set it to a very large number.

If you’re on a machine with resource constraints, it might be a good idea to occasionally archive old .bash_history files to speed up login and reduce memory footprint.

Don’t store specific lines

You can prevent commands that start with a space from going into history by setting $HISTCONTROL to ignorespace . You can also ignore duplicate commands, for example repeated du calls to watch a file grow, by adding ignoredups . There’s a shorthand to set both in ignoreboth .

HISTCONTROL=ignoreboth

You might also want to remove the use of certain commands from your history, whether for privacy or readability reasons. This can be done with the $HISTIGNORE variable. It’s common to use this to exclude ls calls,job control builtins like bg and fg , and calls to history itself:

HISTIGNORE='ls:bg:fg:history'

Record timestamps

If you set $HISTTIMEFORMAT to something useful, Bash will record the timestamp of each command in its history. In this variable you can specify the format in which you want this timestamp displayed when viewed with history . I find the full date and time to be useful, because it can be sorted easily and works well with tools like cut and awk .

HISTTIMEFORMAT='%F %T '

Use one command per line

To make your .bash_history file a little easier to parse, you can force commands that you entered on more than one line to be adjusted to fit on only one with the cmdhist option:

shopt -s cmdhist

Store history immediately

By default, Bash only records a session to the .bash_history file on disk when the session terminates. This means that if you crash or your session terminates improperly, you lose the history up to that point. You can fix this by recording each line of history as you issue it, through the $PROMPT_COMMAND variable:

PROMPT_COMMAND='history -a'

Recommend

  • 30
    • os.51cto.com 4 years ago
    • Cache

    善用Bash history 命令

    相信大家平时都有用 history 命令来查看命令历史记录,但是实际上 history 命令并非只有这个功能,history 还有很多有用的功能。尤其是 Bash 版本的 history 命令,它所提供的功能比所有其他的 Linux Shell history 命令所提供的都要多。

  • 13

    Seven God-Like Bash History Shortcuts You Will Actually Use zwischenzugs Uncategori...

  • 14
    • www.devdungeon.com 4 years ago
    • Cache

    Create Script from Bash History

    Create Script from Bash History Submitted by NanoDano on Wed, 11/21/2012 - 11:04...

  • 5
    • www.lujun9972.win 3 years ago
    • Cache

    如何禁用bash中的history

    如何禁用bash中的history history能记录输入过的命令,这可能带来一些安全隐患,如果不想记录曾经输入过的命令该怎么办呢?难道只能不断的用 history -c 来清理掉吗? 其实无需这么麻烦,有几个方法可以禁用bash中的history...

  • 9
    • blog.henix.info 3 years ago
    • Cache

    从 bash_history 得到 alias 建议

    最后更新日期:2013-11-20   我们经常喜欢在 bash 中定义 alias 或者写一些小函数,以自动化某些常见任务。但这就有一个问题:哪些命令最需要定义成 alias?   考虑两个因素: 命令使用的频率 命令的长度 ...

  • 10
    • blog.eastonman.com 3 years ago
    • Cache

    Bash History的使用技巧

    Bash是常见的shell环境,大多数Linux发行版也都带有Bash,因此高效地利用history可以节省手动输入重复命令的时间。本文分享一下几个我自己使用的技巧和配置。 这个技巧大家应该多少都知道,就是在输入界面按Ctrl-R就可以进行搜索了...

  • 12

    Tutorial How To Use Bash History Commands and Expansions on a Linux VPS Linux Basics

  • 6
    • gist.github.com 2 years ago
    • Cache

    Synchronize history across bash sessions

    This script sounds very interesting. But what am I supposed to do with the script? What do I have to configure how to use this? Thanks...

  • 10

    Francis T. O'Donovan Senior Data Science Manager at Hospital IQ – Planet discoverer, researcher, dev...

  • 5
    • hackaday.com 1 year ago
    • Cache

    Linux Fu: Supercharge Bash History

    Linux Fu: Supercharge Bash History ...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK