5

I am facing an issue in which the commands that I run inside the tmux terminal are not found when I exit that tmux session. Can, anyone please help?

tmux - using this command to run tmux session

echo "Hello" - using this command in tmux session

history - In this case echo "Hello" command is present.

exit - using this to exit the tmux session

history - running this command after exiting from tmux session

After running this I am not able to get echo "Hello" command that I used in tmux session.

Kevin Martin
  • 73
  • 1
  • 5

1 Answers1

6

Exit the terminal and restart it. Now, history will show you all these commands, both from within tmux as before.

The history of a session is only committed to the .bash_history file once you exit a session. When you start a new session, .bash_history is read and the command will be available. The reason it works like this is that it allows to evaluate the history of each session separately.

It is possible to configure your terminal to immediately have issued commands available in the history of all sessions.

From here:

Add the following to your ~/.bashrc:

# Avoid duplicates
HISTCONTROL=ignoredups:erasedups # Ubuntu default is ignoreboth
# When the shell exits, append to the history file instead of overwriting it
shopt -s histappend  # In Ubuntu this is already set by default

After each command, append to the history file and reread it

PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r"

Learn more about the HISTCONTROL variable here.

In a default Ubuntu install, in fact this command alone will do fine:

PROMPT_COMMAND="history -a; history -c; history -r"

Note that any new command from a different session will be available in your session after you "refreshed" the prompt: the history is updated as soon as a prompt is created.

vanadium
  • 88,010