I create a few aliases in terminal like below:

alias k=kubectl
source <(kubectl completion bash)

And then I open a tmux session with command: tmux.

Now inside of tmux pane, when I try using the alias and expect the behaviour of source command run in terminal, it fails! :(

Why does this happen and how do I get it right?

The context is that I end up using multiple panes in tmux and I don't want to re-peat the same set of aliases creation again and again as I open every tmux pane. Possible?

  • 2
    In short, aliases are not inherited by the shell that is started via tmux. You would have to instantiate the aliases in the tmux pane. – Kusalananda Sep 23 '19 at 18:40
  • Have appended the context just so you can answer it as well. Also, it would be great you write your answer as answer instead of as comment, just so I can mark it as correct answer to aid future readers of this question :) – karthiks Sep 23 '19 at 18:56
  • The answer is already given, to the duplicate question. – Kusalananda Sep 23 '19 at 19:22
  • 1
    another point to notice about tmux is that it will start login shells by default, ie its shells won't source ~/.bashrc unless that file is sourced from ~/.bash_profile. – mosvy Sep 24 '19 at 5:58
  • 1
    I put my aliases & everything bash-specific in ~/.bashrc, everything shell-portable in ~/.profile and have a ~/.bash_profile which just sources them both. FWIW, ubuntu, debian and derived are special because they will also source /etc/bash.bashrc before ~/.bashrc (which is not the default upstream and does not happen on other systems like rhel or centos). – mosvy Sep 24 '19 at 14:44

tmux is just a program that runs other programs, so tmux really has nothing to do with this, other than that it starts a new bash session for you. Test this: Do alias t='echo hi' in a terminal, then run the command bash to start a new shell (in the same terminal). The new t alias is not available in the new shell. Aliases are not inherited. Only environment variables are inherited (and a few other things, like what the current directory is).

In short, aliases are not inherited by the shells that are started via tmux. You would have to instantiate the aliases in each tmux pane (possibly by adding the alias definitions to you ~/.bashrc file).

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.