InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Setting Shell History Options in bash
Bash

Setting Shell History Options in bash

InfinityCoder April 5, 2017

You’d like more control over your command-line history.

Set the $HIST* variables and shell options as desired.

The $HISTFILESIZE variable sets the number of lines permitted in the $HISTFILE. T

he default for $HISTSIZE is 500 lines, and $HISTFILE is ~/.bash_history unless you are in
POSIX mode, in which case it’s ~/.sh_history. Increasing $HISTSIZE may be useful, and unsetting it causes the $HISTFILE length to be unlimited.

Changing $HISTFILE probably isn’t necessary, except that if it is not set or the file is not writable, no history will be written to disk.

The $HISTSIZE variable sets the number of lines permitted in the history stack in memory.
$HISTIGNORE and $HISTCONTROL control what goes into your history in the first place.
$HISTIGNORE is more flexible since it allows you to specify patterns to decide what command lines to save to the history. $HISTCONTROL is more limited in that it supports only the few keywords listed here (any other value is ignored):
ignorespace
Command lines that begin with a space character are not saved in the history list.
ignoredups
Command lines that match the previous history entry are not saved in the history list.
ignoreboth
Shorthand for both ignorespace and ignoredups.

erasedups
All previous command lines that match the current line are removed from the history list before that line is saved.
If $HISTCONTROL is not set, or does not contain any of these keywords, all commands are saved to the history list, subject to processing $HISTIGNORE.

The second and subsequent lines of a multiline compound command are not tested, and are added to the history regardless of the value of $HISTCONTROL.
(Material in the preceding paragraphs has been adapted from Edition 2.5b of The GNU Bash Reference Manual for bash Version 2.05b, last updated July 15, 2002; http://www.gnu.org/software/bash/manual/bashref.html.)
As of bash version 3, there is a fascinating new variable called $HISTTIMEFORMAT.

If set and non-null, it specifies an strftime format string to use when displaying or writing the history.

If you don’t have bash version 3, but you do use a terminal with a scrollback buffer, adding a date and time stamp to your prompt can also be very helpful.
Watch out because stock bash does not put a trailing space after the format, but some systems (e.g., Debian) have patched it to do so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
bash-3.00# history
   1 ls -la
   2 help history
   3 help fc
   4 history
 
# Ugly
bash-3.00# export HISTTIMEFORMAT='%Y-%m-%d_%H:%M:%S'
 
bash-3.00# history
   1 2006-10-25_20:48:04ls -la
   2 2006-10-25_20:48:11help history
   3 2006-10-25_20:48:14help fc
   4 2006-10-25_20:48:18history
   5 2006-10-25_20:48:39export HISTTIMEFORMAT='%Y-%m-%d_%H:%M:%S'
   6 2006-10-25_20:48:41history
 
# Better
bash-3.00# HISTTIMEFORMAT='%Y-%m-%d_%H:%M:%S; '
 
bash-3.00# history
   1 2006-10-25_20:48:04; ls -la
   2 2006-10-25_20:48:11; help history
   3 2006-10-25_20:48:14; help fc
   4 2006-10-25_20:48:18; history
   5 2006-10-25_20:48:39; export HISTTIMEFORMAT='%Y-%m-%d_%H:%M:%S'
   6 2006-10-25_20:48:41; history
   7 2006-10-25_20:48:47; HISTTIMEFORMAT='%Y-%m-%d_%H:%M:%S; '
   8 2006-10-25_20:48:48; history
 
# Getting tricky now
bash-3.00# HISTTIMEFORMAT=': %Y-%m-%d_%H:%M:%S; '
 
bash-3.00# history
   1 : 2006-10-25_20:48:04; ls -la
   2 : 2006-10-25_20:48:11; help history
   3 : 2006-10-25_20:48:14; help fc
   4 : 2006-10-25_20:48:18; history
   5 : 2006-10-25_20:48:39; export HISTTIMEFORMAT='%Y-%m-%d_%H:%M:%S'
   6 : 2006-10-25_20:48:41; history
   7 : 2006-10-25_20:48:47; HISTTIMEFORMAT='%Y-%m-%d_%H:%M:%S; '
   8 : 2006-10-25_20:48:48; history

The last example uses the : built-in with the ; metacharacter to encapsulate the date stamp into a “do nothing” command (e.g., : 2006-10-25_20:48:48;).

This allows you to reuse a literal line from the history file without having to bother parsing out the
date stamp.

Note the space after the : is required. There are also shell options to configure history-file handling.

If histappend is set, the shell appends to the history file; otherwise it overwrites the history file.

Note that it is still truncated to $HISTSIZE. If cmdhist is set, multiline commands are saved as a single line, with semicolons added as needed.

If lithist is set, multiline commands are saved with embedded newlines.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Getting bash Without Getting bash
You want to try out a shell or a shell …

Getting bash Without Getting bash

Avoiding Interpreter Spoofing in bash
You want to avoid certain kinds of setuid root spoofing …

Avoiding Interpreter Spoofing in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap