Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Convert Bash history to Zsh history
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This is how I used it:
# $ cat ~/.bash_history | python >> ~/.zsh_history
import sys
import time
def main():
timestamp = None
for line in sys.stdin.readlines():
line = line.rstrip('\n')
if line.startswith('#') and timestamp is None:
t = line[1:]
if t.isdigit():
timestamp = t
sys.stdout.write(': %s:0;%s\n' % (timestamp or time.time(), line))
timestamp = None
if __name__ == '__main__':
Copy link

fernandoacorreia commented Sep 5, 2017

Worked well for me on Ubuntu 16.04. Thanks.

Copy link

ghost commented Jul 4, 2018

Like a charm on Ubuntu 17.10 too!

Copy link

sepehrmm commented Dec 26, 2018

worked on Ubuntu 14.04, Thanks.

Copy link

majidalavizadeh commented Feb 13, 2019

Worked like a charm on Mac

Copy link

brnl commented Mar 7, 2019

Just an update that this one works really well :)
Ubuntu 18.10

Copy link

dijotmathews commented Sep 12, 2019

works on ubuntu 19.04 python 2.7.16
GNU bash, version 5.0.3(1)-release (x86_64-pc-linux-gnu)
zsh 5.5.1 (x86_64-ubuntu-linux-gnu)

Copy link

BeebBenjamin commented Sep 20, 2019

Works for me on Ubutnu 18.04!

Copy link

awthwathje commented Nov 8, 2019

Debian Buster, works good.

Copy link

michaelklos commented Dec 29, 2019

Worked like a charm (Ubuntu 18.04). Thanks!

Copy link

fbraz3 commented Mar 11, 2020

OSX Catalina, works fine! =D
Python 2.7.16

Copy link

fahimio commented May 8, 2020

Nice!! Worked beautifully!

Copy link

Kl0ven commented Jun 18, 2020

Thanks !!!! worked on WSL - python3.8

Copy link

dexios1 commented Jul 27, 2020

You're awesome @muendelezaji. Thanks for this. Worked so well.

Copy link

ahmedhr commented Oct 6, 2020


Copy link

mc256 commented Dec 10, 2020


Copy link

evanjt commented Feb 8, 2021

Great, thanks :)

Copy link

mrkeuz commented Jun 22, 2021

Armbian 21.02.3 Focal (Orange Pi) worked! Thanks!

Copy link

piranha87 commented Aug 10, 2021

works well on ubuntu 20.04

Copy link

bernardolm commented Aug 11, 2021


Copy link

blooddrainer commented Sep 14, 2021

Works fine on Manjaro! Thanks!

Copy link

gunturaf commented Sep 28, 2021


Copy link

au8st commented Nov 18, 2021

Working on Debian 11 (stock) python, either run the script as:

cd ~/ && cp .bash_history .bash_history.bak && cat ~/.bash_history | python3 >> ~/.zsh_history

Or, symlink python3 > python:

sudo ln -s /usr/bin/python3.9 /usr/bin/python
cat ~/.bash_history | python >> ~/.zsh_history

Copy link

PeterPerhac commented Nov 20, 2021

nice, worked like a charm on my new MacBook Pro. 👍 thank you

Copy link

ricarhincapie commented Jan 4, 2022

Perfect. Thanks!

Copy link

mbency commented Jan 9, 2022

thanks, worked on a new M1 MBP

Copy link

devrimbaris commented Feb 23, 2022

Thanks, worked on Catalina with python 2.7.16

Copy link

Zackhardtoname commented Apr 7, 2022

To get it to work with hstr, I had to use int(time.time())
Thanks for saving my history!!

Copy link

mzpwr commented Jun 1, 2022

On WSL had pretty big history and was using HSTR, so after importing the history had 2 problems:

  1. Error zsh: corrupt history file /home/user/.zsh_history
  2. HSTR showing timestamps
    So the solution for me was modified script with thanks to @Zackhardtoname and then several additional steps:
  3. Write date to some other file .zsh_history_tmp
  4. strings .zsh_history_tmp > .zsh_history
  5. fc -R .zsh_history

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment