zellij-org / zellij

A terminal workspace with batteries included
https://zellij.dev
MIT License
21.14k stars 643 forks source link

Source ~/.bash_profile on startup #2586

Open ashee opened 1 year ago

ashee commented 1 year ago

When I launch zellij, I would like for it to source .bash_profile. How do I do it?

Is there anyways I could pass an argument within ~/.config/zellij/config.kdl. I have the following

// Choose the path to the default shell that zellij will use for opening new panes
// Default: $SHELL
Default: "/opt/homebrew/bin/bash"
ashee commented 1 year ago

My current workflow is the following

bash-5.2$
bash-5.2$ exec bash -il
(sci) ashee-m1-515:~ amitava$
jade-tea-h commented 1 year ago

.bash_profile is usually only sourced by login shells, which should happen once on startup, but I'm not sure if OSX has different behavior. If you want something sourced in every instance of the shell, add it to .bashrc. If you want zellij to source .bash_profile a single time on opening, maybe use a layout?

Just realized you are saying you start bash with the command exec bash -il, I'd recommend maybe trying this in your config -- default_shell "/opt/homebrew/bin/bash -il. Then .bash_profile and .bashrc will be sourced automatically. If you want .bash_profile sourced only a single time, try opening zellij with zellij run source ~/.bash_profile or zellij --layout my_startup_layout and have my_startup_layout in your layout directory with:

layout {
  pane command="source ~/.bash_profile"
}
kristian-clausal commented 1 year ago

What is the difference between how Zellij and tmux handle bash_profile and bashrc? If I remove my PS1 stuff from .bash_profile, the prompt returns to default colorless, while it's the opposite for Zellij.

jade-tea-h commented 1 year ago

@kristian-clausal that is because tmux by default runs a login shell. Honestly, I'm not sure why and it seems somewhat strange. Anyway, .bash_profile is only sourced in login shells, which is normally just the first shell, probably run in the background of your login screen. So, if you make the same change and reboot, you should see the same change in zellij as tmux. Tmux just runs a login shell that sources .bash_profile every new instance.

voioo commented 5 months ago

If you are on a Mac, try having both .bash_profile and .bashrc files with same contents. It should work.

jcayzac commented 4 months ago

On macOS, interactive terminals start login shells.

The various workarounds offered in this thread (creating a .bashrc symlink to .bash_profile, or starting invoking bash -il) don't work because the full environment is inherited by the subshell, so any configuration that expects a clean login shell will fail.

As an example:

# .bash_profile
[ -z "$BASH_PROFILE_SOURCED" ] || {
    echo >&2 error ".bash_profile is already sourced."
    return 1
}

Starting bash -il in zellij will show the error message and not setup bash correctly. The correct way to invoke bash -il so that it configures itself like a login shell is to first discard all the environment except for HOME, SHELL, PATH, TERM and USER (see su -l).

If zellij allowed to pass a full command line for default_shell, as well as an allow list for what environment variables to keep, things could work as expected on macOS:

default_shell "/opt/homebrew/bin/bash -il"
env_allow "HOME SHELL PATH TERM USER"

Edit: Actually, the following command line is enough to start a bash login shell properly:

/usr/bin/env -i HOME="$HOME" SHELL="$SHELL" PATH="$PATH" TERM="$TERM" USER="$USER" bash -il