Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I often find myself switching between Bash and Zsh, and using the history search functionality to recover a command.

However, since Bash and Zsh have different history files, I often find that the command I'm searching for has been executed in the other shell.

Is there any way to share or synchronize history between the two?

share|improve this question
1  
The syntax of bash and zsh is different enough that you'd end up with many commands that don't work when copied to the other shell. – Gilles Sep 19 '11 at 23:41
up vote 4 down vote accepted

If you are using the defaults for bash and zsh:

$ cat ~/.histfile >> ~/.bash_history
$ youreditor ~/.zshrc
# Here change your config to:
HISTFILE=~/.bash_history
$ rm ~/.histfile

Now you have the same file for history in both shells.

share|improve this answer
1  
Does this really work?? The two histfiles have completely different formats! – Neil Traft Jul 12 '14 at 7:48
    
Yes, both shells use the same format. One command per line. – Rufo El Magufo Jul 13 '14 at 18:32
1  
My .zsh_history has lines like : 1399608924:0;hg diff whereas my .bash_history has simply hg diff. Maybe it's because I'm using oh-my-zsh? – Neil Traft Jul 14 '14 at 23:58
    
Maybe. Or some option of zsh. I don't know. – Rufo El Magufo Jul 16 '14 at 0:03
1  
you need to do unsetopt EXTENDED_HISTORY to get bash-compatible command-only history. – Matija Nalis Feb 2 at 22:54

Not exactly what you were looking for, but in order to import from bash to zsh, you can use this node.js script.

   // This is how I used it:
   // $ node bash-history-to-zsh-history.js >> ~/.zsh_history

   var fs = require("fs");
   var a = fs.readFileSync(".bash_history");
   var time = Date.now();
   a.toString().split("\n").forEach(function(line){
     console.log(": "+ (time++) + ":0;"+line);
   });

Source

share|improve this answer

In response to Elad, people may have .bash_history files that have an extra line before each command that starts with (#) and has trailing digits following (123456789), for example: #123456789. If your bash_history file has these extra lines, use this modified version of Elad's code to process a clean zsh formatted history to use. Thanks Elad for the quick conversion code.

/*
 * You should backup your .bash_history file first doing this:
 * $ cp ~/.bash_history ~/.bash_history.backup
 * 
 * create the .js file to use first:
 * $ touch ~/.bash-history-to-zsh-history.js
 *
 * This is how I use it based on Elads example:
 * $ node ~/.bash-history-to-zsh-history.js >> ~/.zsh_history
 *
 **/

var fs = require("fs");
var a = fs.readFileSync(".bash_history");
var time = Date.now();
a.toString().split("\n").forEach(function(line){
  if (line.indexOf("#")!=0) console.log(": "+ (time++) + ":0;"+line);
});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.