Sample Header Ad - 728x90

Unix & Linux Stack Exchange

Q&A for users of Linux, FreeBSD and other Unix-like operating systems

Latest Questions

-2 votes
2 answers
50 views
HISTTIMEFORMAT not working as desired in RHEL 8 bash 4.4.20
so trying to capture history with date and readable timestamps AND the command should appear on same line. following is failing: Bash ver is: GNU bash, version 4.4.20(1)-release (x86_64-redhat-linux-gnu) OS: RHEL 8.x in .bashrc.... setopt EXTENDED_HISTORY export HISTTIMEFORMAT='%F %I:%M:%S %T' #expo...
so trying to capture history with date and readable timestamps AND the command should appear on same line. following is failing: Bash ver is: GNU bash, version 4.4.20(1)-release (x86_64-redhat-linux-gnu) OS: RHEL 8.x in .bashrc.... setopt EXTENDED_HISTORY export HISTTIMEFORMAT='%F %I:%M:%S %T' #export HISTTIMEFORMAT="%F %T " export HISTSIZE=1000 export HISTFILE=$HOME/.bash_history-$USER export HISTFILESIZE=1000 export PROMPT_COMMAND='history -a' env variables: $ env|grep HIST HISTCONTROL=ignoredups HISTTIMEFORMAT=%F %I:%M:%S %T HISTFILE=/home/user1/.bash_history-user1 HISTSIZE=1000 HISTFILESIZE=1000 the records appear as: #1753745611 cd #1753745616 ls -ltra|tail #1753745626 cat .profile #1753745633 cat .kshrc **expected:** Mon Jul 28 23:33:31 GMT 2025 cd Mon Jul 28 23:33:36 GMT 2025 ls -ltra|tail Mon Jul 28 23:33:46 GMT 2025 cat .profile Mon Jul 28 23:33:53 GMT 2025 cat .kshrc two problems with this. 1. Timestamp appears in UNIX EPOCH format. 2. Timestamp and command appears on separate lines. They should be together. Also behaves the same way when using KSH. How can this be fixed? preferably using HISTTIMEFORMAT Thank you.
Rajeev (256 rep)
Jul 29, 2025, 03:33 PM • Last activity: Jul 29, 2025, 10:01 PM
74 votes
14 answers
155386 views
Delete last N lines from bash history
When accidentally pasting a file into the shell it puts a ton of ugly nonsense entries in the bash history. Is there a clean way to remove those entries? Obviously I could close the shell and edit the `.bash_history` file manually but maybe there's some kind of API available to modify the history of...
When accidentally pasting a file into the shell it puts a ton of ugly nonsense entries in the bash history. Is there a clean way to remove those entries? Obviously I could close the shell and edit the .bash_history file manually but maybe there's some kind of API available to modify the history of the current shell?
ThiefMaster (2407 rep)
Feb 17, 2013, 12:40 PM • Last activity: Jul 15, 2025, 09:15 PM
4 votes
2 answers
258 views
history -cw does not overwrite ~/.bash_history file
I use Arch Linux and bash. I've identified the following and can't understand how it works. The first issue is: `history -cw` doesn't clear `~/.bash_history` file but `history -c ; history -w` does. The second one is: When I open the shell and execute something like these: ```bash $ first $ second $...
I use Arch Linux and bash. I've identified the following and can't understand how it works. The first issue is: history -cw doesn't clear ~/.bash_history file but history -c ; history -w does. The second one is: When I open the shell and execute something like these:
$ first
$ second
$ history -c
$ third
and close the shell, ~/.bash_history is supplemented with a line third. I expect ~/.bash_history to contain the single line third. My shopt histappend is off, doesn't this mean that the history file needs to be overwritten? It's not critical for me, but I want to understand how it works.
hlooo (41 rep)
Jul 6, 2025, 02:42 PM • Last activity: Jul 7, 2025, 07:52 AM
2 votes
2 answers
2732 views
Don't save failed command to history?
I'm using snippet code below to exclude all failed commands (return code 1) from saving to zsh history: ``` zshaddhistory() { whence ${${(z)1}[1]} >| /dev/null || return 1 } ``` But if the command is an alias `lsl` (alias lsl='ls -l') the failed command will still be inserted into zsh history: ``` l...
I'm using snippet code below to exclude all failed commands (return code 1) from saving to zsh history:
zshaddhistory() { whence ${${(z)1}} >| /dev/null || return 1 }
But if the command is an alias lsl (alias lsl='ls -l') the failed command will still be inserted into zsh history:
lsl whatever_folder_doesnt_exist
whatever_folder_doesnt_exist doesn't exist and I observe lsl whatever_folder_doesnt_exist still in zsh history. Here I want to exclude all command that return code is not 0, how can I do that?
raring-coffee20 (1855 rep)
May 6, 2020, 07:54 AM • Last activity: Jun 11, 2025, 09:56 AM
0 votes
2 answers
3537 views
Use reverse i search to cycle through only commands matching pattern
The contents of the command history file using `history | less` is: ``` 555 ls 556 ls -a 557 echo "hello" 558 echo "hello again" 559 cd 560 pwd 561 echo "hello hello" 562 ls 563 echo "hello hello hello" 564 cd 565 pwd ``` I want to use reverse i search ``(reverse-i-search)`echo':`` to cycle through...
The contents of the command history file using history | less is:
555 ls
556 ls -a
557 echo "hello"
558 echo "hello again"
559 cd
560 pwd
561 echo "hello hello"
562 ls
563 echo "hello hello hello"
564 cd
565 pwd
I want to use reverse i search `(reverse-i-search)echo':` to cycle through only commands in the command history file matching the pattern (i.e. in this case \echo') entered on the command line prompt, preferably until a specified line number in the command history file so that I only select previously used echo commands that I want to run again not all echo commands in the command history file Using reverse i search finds the most recent usage of the command echo (hence the name reverse i search i.e. reverse searching for a command). However when scrolling forward in reverse order (i.e. reverse searching) bash scrolls through each line of the command history file starting from the line returned by the reverse i search, thus including commands I don't want to search for. 1. The reverse i search prompt `(reverse-i-search)echo':`` disappears once you start scrolling through the history list. How do you prevent the reverse i search prompt from disappearing during scrolling so that when you scroll forward in a reverse i search only the commands matching the pattern are shown? 2. If 1 is possible then how do you temporarily limit the reverse i search to a specific line number in the command history file in order to only select commands from a section of the command history file? Note: a solution to this question would also be useful for those times when you have several instances of the same command line arguments and reverse i search does not find the one you're looking for
bit (1176 rep)
Jun 25, 2019, 08:14 PM • Last activity: May 26, 2025, 04:06 AM
1 votes
1 answers
58 views
"history-search-backward" but ignore sudo prefix in zsh?
According to this page: https://zsh.sourceforge.io/Doc/Release/Zsh-Line-Editor.html `history-search-backward`: "Search backward in the history for a line beginning with the first word in the buffer." But when command is prefixed with `sudo` that breaks everything. Can I make zsh ignore `sudo` for th...
According to this page: https://zsh.sourceforge.io/Doc/Release/Zsh-Line-Editor.html history-search-backward: "Search backward in the history for a line beginning with the first word in the buffer." But when command is prefixed with sudo that breaks everything. Can I make zsh ignore sudo for this command? Also want to apply same thing for history-search-forward
sloppy (121 rep)
Feb 23, 2025, 04:16 PM • Last activity: May 25, 2025, 09:47 AM
156 votes
5 answers
124649 views
Unlimited history in zsh
In `zsh`, I want to have unlimited history. I set `HISTSIZE=`, which works in `bash`. Now I import an old history mv old_history .history which is pretty big wc -l .history 43562 .history If I now close and start `zsh` again, I see wc -l .history 32234 .history Can't I have unlimited history in `zsh...
In zsh, I want to have unlimited history. I set HISTSIZE=, which works in bash. Now I import an old history mv old_history .history which is pretty big wc -l .history 43562 .history If I now close and start zsh again, I see wc -l .history 32234 .history Can't I have unlimited history in zsh?
pfnuesel (6263 rep)
Apr 2, 2016, 02:36 PM • Last activity: May 24, 2025, 10:16 PM
13 votes
4 answers
6138 views
Create history log per working directory in bash
I was wondering if it is possible to keep a file containing history per current working directory. So for example if I was working in `/user/temp/1/` and typed a few commands, these commands would be saved in `/user/temp/1/.his` or something. I am using bash.
I was wondering if it is possible to keep a file containing history per current working directory. So for example if I was working in /user/temp/1/ and typed a few commands, these commands would be saved in /user/temp/1/.his or something. I am using bash.
tafelplankje (323 rep)
Aug 24, 2016, 06:37 PM • Last activity: May 14, 2025, 02:33 PM
1 votes
2 answers
650 views
ksh: appending date-time in history
Does anyone have a solution to append date and time to this as another column? It seems like it would be very useful with sorting, etc... $ history 3 history Thank you!
Does anyone have a solution to append date and time to this as another column? It seems like it would be very useful with sorting, etc... $ history 3 history Thank you!
Nick (205 rep)
Oct 30, 2019, 07:22 PM • Last activity: May 12, 2025, 09:35 AM
7 votes
2 answers
10295 views
Sending bash history to syslog
Bash version 4.N has apparently the ability to write command-history to syslog, but I can't find information about how to configure this. I have read several pages which offer hacks using the `PROMPT_COMMAND`, and trap, and I know that there's a patch available, but this should be unnecessary, as it...
Bash version 4.N has apparently the ability to write command-history to syslog, but I can't find information about how to configure this. I have read several pages which offer hacks using the PROMPT_COMMAND, and trap, and I know that there's a patch available, but this should be unnecessary, as it is now built in. I know I can use auditd to capture commands, but I'd like to use the bash/syslog combination.
Graham Nicholls (772 rep)
Jul 18, 2018, 11:06 PM • Last activity: May 9, 2025, 08:30 AM
5 votes
1 answers
1932 views
zsh in tabby separate history for each tab
I want to keep history separated for each tab (even for split windows within one tab). I therefore found a solution to add these two lines after `source $ZSH/oh-my-zsh.sh`: source $ZSH/oh-my-zsh.sh unsetopt inc_append_history unsetopt share_history Now the history is really separated, nevertheless w...
I want to keep history separated for each tab (even for split windows within one tab). I therefore found a solution to add these two lines after source $ZSH/oh-my-zsh.sh: source $ZSH/oh-my-zsh.sh unsetopt inc_append_history unsetopt share_history Now the history is really separated, nevertheless when I completely close the terminal (in my case Tabby), the histories for each tab are common again. How to solve that? I use zsh with Tabby.
Arie (151 rep)
May 10, 2022, 01:51 PM • Last activity: May 6, 2025, 08:47 AM
5 votes
0 answers
690 views
Command line history in the fish shell suffering from short term memory issues
Over the last few months, I've been trying out the `fish` shell as my interactive shell. One issue that somewhat irritates me is that the shell occasionally forgets recent commands from its command line history. Let's say I have a personal script in my `$PATH` called `mail-get.sh`. I can run it by t...
Over the last few months, I've been trying out the fish shell as my interactive shell. One issue that somewhat irritates me is that the shell occasionally forgets recent commands from its command line history. Let's say I have a personal script in my $PATH called mail-get.sh. I can run it by typing its name on the command line and then recall the command at a later time by just typing get and pressing Up-arrow. Sometimes (a few times a day), the recall fails, and instead, I get _older_ commands that happen to contain the string get. The older command may be _several months_ older than the most recent invocation of my mail-get.sh script. Using history merge at that point always resolves the issue. Unfortunately, I can't reproduce the issue on purpose. I have not changed how fish should manage its history. My home directory is not network-mounted. I run several shell sessions in different tmux panes at the same time. I'm most heavily using fish version 3.5.1 on FreeBSD. My question is whether this is something that other users of the shell experience too, and whether it has a known cause and a convenient solution, or whether I should report it as a bug to the fish shell project. --- 2025 update: This is just to say that version 3.7.1 on Alpine Linux (the system I'm using the shell on most often nowadays) still exhibits the same behaviour. I see there are newer versions, 4.0.2 being the most recent. It will be interesting to test that once that version trickles down to the Unix I'm using.
Kusalananda (354171 rep)
Sep 8, 2022, 06:59 AM • Last activity: May 5, 2025, 05:44 PM
4 votes
0 answers
83 views
Why is `fc` output different through a pipe in a subshell?
Why does `echo "$(fc -l -1)"` show the previous command, but `echo "$(fc -l -1 | cat)"` show the current command? ```bash $ testfc () { > echo "$(fc -l -1)" > echo "$(fc -l -1 | cat)" > } $ echo foo foo $ testfc 1820 echo foo 1821 testfc ``` ## More detail I was testing things for [a rabbit-hole que...
Why does echo "$(fc -l -1)" show the previous command, but echo "$(fc -l -1 | cat)" show the current command?
$ testfc () {
>     echo "$(fc -l -1)"
>     echo "$(fc -l -1 | cat)"
> }

$ echo foo
foo
$ testfc
1820     echo foo
1821     testfc
## More detail I was testing things for [a rabbit-hole question](https://unix.stackexchange.com/questions/794387/multiline-command-substitution-syntax-errors-with-mysterious-1) and ended up down another rabbit hole. I created this function to try different ways of accessing the previous or current command history number with the [fc] and [history] built-ins:
testfc () {
    printf 'fc1\t';        fc -l -1
    printf 'fc1|\t';       fc -l -1 | cat
    printf '(fc1)\t%s\n'   "$(fc -l -1)"
    printf '(fc1|)\t%s\n'  "$(fc -l -1 | cat)" # this one is weird
    printf 'fc0\t';        fc -l -0
    printf 'fc0|\t';       fc -l -0 | cat
    printf '(fc0)\t%s\n'   "$(fc -l -0)"
    printf '(fc0|)\t%s\n'  "$(fc -l -0 | cat)"
    printf 'hist\t';       history 1
    printf 'hist|\t';      history 1 | cat
    printf '(hist)\t%s\n'  "$(history 1)"
    printf '(hist|)\t%s\n' "$(history 1 | cat)"
    str='\!'
    printf '@P\t%s\n'      "${str@P}"
    printf 'HC\t%s\n'      "$HISTCMD"
}
Generally, fc -l -0 & history 1 show the current command, and fc -l -1 shows the previous command. Their outputs don't change when run in a $() subshell or piped through cat. *Except* "$(fc -l -1 | cat)"!
1831 $ echo foo
foo

1832 $ testfc
fc1     1831     echo foo
fc1|    1831     echo foo
(fc1)   1831     echo foo
(fc1|)  1832     testfc   # <-- WHAT?
fc0     1832     testfc
fc0|    1832     testfc
(fc0)   1832     testfc
(fc0|)  1832     testfc
hist     1832  2025-05-02 15:10:59  testfc
hist|    1832  2025-05-02 15:10:59  testfc
(hist)   1832  2025-05-02 15:10:59  testfc
(hist|)  1832  2025-05-02 15:10:59  testfc
@P      1832
HC      1832

1833 $ fc -l -2
1831     echo foo
1832     testfc
## Context
$ echo $BASH_VERSION
5.2.37(1)-release
$ type fc
fc is a shell builtin
$ type history
history is a shell builtin
$ type cat
cat is /usr/bin/cat
Jacktose (533 rep)
May 2, 2025, 10:18 PM • Last activity: May 3, 2025, 09:24 PM
0 votes
1 answers
41 views
local zsh history on up-arrow, global zsh-history on ctrl-R
I am looking for a very specific behavior in my [Zsh](https://en.wikipedia.org/wiki/Z_shell) terminals. I have multiple terminal windows open at the same time. On each terminal window, if I hit the up arrow, I'd like for it to go up through the **local** history of that specific terminal window. On...
I am looking for a very specific behavior in my [Zsh](https://en.wikipedia.org/wiki/Z_shell) terminals. I have multiple terminal windows open at the same time. On each terminal window, if I hit the up arrow, I'd like for it to go up through the **local** history of that specific terminal window. On each terminal window, if I type Ctrl-R and then start typing to search through history, I would like it to search through the **global** history that comes from all the terminal windows combined. Is it possible? How can I do it?
mareoraft (649 rep)
Apr 23, 2025, 04:35 PM • Last activity: May 1, 2025, 07:59 AM
47 votes
7 answers
23201 views
Why do I lose my ZSH history?
Every once in a while I discover my `zsh` history has been truncated (or maybe lost entirely, difficult to tell), and I have to restore it from backup. For example, today: ``` ls -lh ~/.zsh_history -rw------- 1 stripe staff 32K 21 Feb 10:20 /Users/stripe/.zsh_history ``` But in my backup from a few...
Every once in a while I discover my zsh history has been truncated (or maybe lost entirely, difficult to tell), and I have to restore it from backup. For example, today:
ls -lh ~/.zsh_history
-rw-------  1 stripe  staff    32K 21 Feb 10:20 /Users/stripe/.zsh_history
But in my backup from a few days ago:
-rw-------@  1 stripe  staff  203K 17 Feb 22:36 /Volumes/Time Machine Backups/.../Users/stripe/.zsh_history
I have configured zsh to save lots of history so it shouldn't be a matter of the shell intentionally trimming the file.
unsetopt share_history
setopt inc_append_history
setopt hist_ignore_all_dups
HISTSIZE=500000
SAVEHIST=$HISTSIZE
Has anyone else experienced this and found a way to mitigate it? Is there some zsh spring cleaning feature I'm unaware of?
Alexander Ljungberg (639 rep)
Feb 21, 2020, 10:34 AM • Last activity: Apr 17, 2025, 06:51 AM
5 votes
2 answers
14261 views
How can I view complete zsh history?
I am using zsh on MacOS BigSur. If I write `history`, I get the last 15 commands. How can I view the complete history?
I am using zsh on MacOS BigSur. If I write history, I get the last 15 commands. How can I view the complete history?
serax (249 rep)
Jul 11, 2021, 01:32 PM • Last activity: Apr 3, 2025, 10:02 AM
7 votes
3 answers
1126 views
How to properly protect .bash_history against truncation?
What can I do so that my `.bash_history` is only aver appended to? I am working in a lot of environments, like `singularity`, `apptainer`, `docker`, `podman` and ssh with various settings and mounts. Each of the environments may have different `HISTFILESIZE` value, there might be something in /etc/p...
What can I do so that my .bash_history is only aver appended to? I am working in a lot of environments, like singularity, apptainer, docker, podman and ssh with various settings and mounts. Each of the environments may have different HISTFILESIZE value, there might be something in /etc/profile.d on different machines, and I have my own HISTFILE=.bash_historymy HISTFILESIZE="" HISTSIZE="" set in my ~/.bashrc and ~/.profile and ~/.bash_profile. The problem is, if _one_ of the environments happens to set HISTFILESIZE, bam - my .bash_history is lost. I am setting my own HISTFILESIZE in my .bashrc, but if I happen to source /nfs/my/friends/.profile or other scripts they will override HISTFILESIZE and not touch HISTFILE, so still my separate HISTFILE will be truncated. Once it happens, I lose my history. This has happened to me many times throughout the years on various systems and environments. What I had, for quite some time, is a script in ~/.bashrc that would create a number of .bash_history backups and rotate them, this was accompanied with some size checking, like:
name="$(basename "$HISTFILE")"
backupdir=~/.cache/bash_history/
mkdir -p "$backupdir"
file="$HISTFILE"
for ((i=1;i<5;++i)); do
   cp "$file" "$backupdir/$name.$i"
   file="$backupdir/$name.$i"
done
So I would have 5 "sessions" to restore the history file or backup it or "cat" it together. I feel like this was an unsatisfactory temporary solution. Is there a proper solution to protect against bash truncating .bash_history? I would imagine one could run "diff" against old and new one and only merge the missed chunks from the back of .bash_history. I do not think making .bash_history a fifo is a good solution. For example, I would be happy if a shell would "restore" the lost truncated commands on the next ~/.bashrc run from a backup file.
KamilCuk (970 rep)
Mar 14, 2025, 11:34 AM • Last activity: Mar 14, 2025, 08:33 PM
-1 votes
1 answers
70 views
"history -0" works in ksh, why doesn't "history -0" work in zsh?
Why does `ksh` `history -0` (last command which is the history command itself) work, yet `zsh` it fails (only `history -1` avail on my zsh). I tried `zsh -f` to use default options and it still fails. I'm using ksh version `Version AJM 93u+ 2012-08-01` and Zsh v5.9 on MacOS. This question is about t...
Why does ksh history -0 (last command which is the history command itself) work, yet zsh it fails (only history -1 avail on my zsh). I tried zsh -f to use default options and it still fails. I'm using ksh version Version AJM 93u+ 2012-08-01 and Zsh v5.9 on MacOS. This question is about the history command and not preexec() to get the current command.
atod (155 rep)
Jan 31, 2025, 04:51 AM • Last activity: Mar 10, 2025, 07:33 AM
13 votes
2 answers
11806 views
Search bash history for already typed command
When I type ctrl+r and *then* start typing I can see what commands in the history match which is great. Now is there a way to search the history on commands I have *already* typed in the terminal? For example if I type ctrl+r and then type ping I can cycle through servers I have pinged. But if I typ...
When I type ctrl+r and *then* start typing I can see what commands in the history match which is great. Now is there a way to search the history on commands I have *already* typed in the terminal? For example if I type ctrl+r and then type ping I can cycle through servers I have pinged. But if I type "ping" first and then hit ctrl+r it ignores the "ping" I have already typed. Some times I'll get half way though typing out a string of commands and then think "oh I already typed this it sure would be nice to search the history on what I have already typed instead of starting over". Does this make sense what I am asking?
user1028270 (1104 rep)
Mar 5, 2016, 01:20 AM • Last activity: Feb 18, 2025, 03:44 AM
121 votes
15 answers
57551 views
How can I remove duplicates in my .bash_history, preserving order?
I really enjoying using `control+r` to recursively search my command history. I've found a few good options I like to use with it: # ignore duplicate commands, ignore commands starting with a space export HISTCONTROL=erasedups:ignorespace # keep the last 5000 entries export HISTSIZE=5000 # append to...
I really enjoying using control+r to recursively search my command history. I've found a few good options I like to use with it: # ignore duplicate commands, ignore commands starting with a space export HISTCONTROL=erasedups:ignorespace # keep the last 5000 entries export HISTSIZE=5000 # append to the history instead of overwriting (good for multiple connections) shopt -s histappend The only problem for me is that erasedups only erases sequential duplicates - so that with this string of commands: ls cd ~ ls The ls command will actually be recorded twice. I've thought about periodically running w/ cron: cat .bash_history | sort | uniq > temp.txt mv temp.txt .bash_history This would achieve removing the duplicates, but unfortunately the order would not be preserved. If I don't sort the file first I don't believe uniq can work properly. How can I remove duplicates in my .bash_history, preserving order? ### Extra Credit: Are there any problems with overwriting the .bash_history file via a script? For example, if you remove an apache log file I think you need to send a nohup / reset signal with kill to have it flush it's connection to the file. If that is the case with the .bash_history file, perhaps I could somehow use ps to check and make sure there are no connected sessions before the filtering script is run?
cwd (46887 rep)
Sep 20, 2012, 02:55 PM • Last activity: Feb 3, 2025, 01:47 PM
Showing page 1 of 20 total questions