Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Often times I've typed in a bunch of commands when I realize that I am going to keep typing them in and that I would really like a script.

Now I understand that no matter how I save my history, I am going to have to edit it to clean things up, but would like to hear suggestions on how to save my history so that it is close to a usable script.

share|improve this question

3 Answers 3

The way i'd approach this is:

history -a - add new lines to history file

ctrl-x, ctrl-e - open an editor to make a quick script (in vi, for me)

:r ~/.bash_history | tail -n 20 - read in the last 20 commands from history file.

share|improve this answer

If you type history in the shell, you will see a list of recent commands. You can see this to a script by history > my_script or something (or just copy it from the ~/.bash_history file).

From the my_script file you can remove the unnecessary commands, add the shebang line, typically #!/bin/bash, and make it executable chmod +x my_script and you're done.

share|improve this answer

A different approach could be to use an editor with a macro language.

  • Import a bunch of lines with cut and paste
  • Filter out output of the commands by grepping for your prompt
  • Remove the prompt from the beginning of lines
  • insert a shebang at the beginning of the file

A significant prompt will help to make this work in most cases. For session sizes of more than a few screen pages, it will not be very comfortable.

share|improve this answer

Your Answer


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.