4

Can I start xterm with a custom bash history including a list of predefined commands? So that I can run several xterm, where each instance has his own history.

1 Answer 1

4
  1. Make a file ~/myhistory with the commands you want in the history, one per line
  2. chmod a-w ~/myhistory, so that Bash won't try to change it. If you want the commands the user enters to be written back (to that same file), skip this step.
  3. HISTFILE=~/myhistory xterm -e bash to make Bash load history data from ~/myhistory. If your default shell is bash then you can lose the -e bash.

Within the shell the command history will be what you wrote in the file, accessible through the up arrow, history expansion, the fc command, or any other way you get to the history.


If you can't make a real file, this fairly-hacky workaround will work as well:

HISTFILE=<(printf '%s\n' command1 "second 'command here'" 3rd) xterm -e bash

This uses process substitution to generate a file-like entity for the shell to read from, with the output of the printf command as the contents. This printf will print out each of the arguments, one per line. You'll have to quote things carefully to make sure that the commands as output are in the format you need.

4
  • Does this also work, if I want to start several xterm, where each has his own history file?
    – Hölderlin
    May 12, 2017 at 5:16
  • As long as you use a different file for each one, yes. May 12, 2017 at 5:17
  • So I can set (override) the environment variable HISTFILE for each bash instance when they run simultaneously without loosing the link between one bash instance and their history file?
    – Hölderlin
    May 12, 2017 at 5:27
  • Yes, you can do that. May 12, 2017 at 5:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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