Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Source ~/.bash_profile on startup #2586

Open
ashee opened this issue Jun 28, 2023 · 6 comments
Open

Source ~/.bash_profile on startup #2586

ashee opened this issue Jun 28, 2023 · 6 comments

Comments

@ashee
Copy link

ashee commented Jun 28, 2023

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
Copy link
Author

ashee commented Jun 28, 2023

My current workflow is the following

bash-5.2$
bash-5.2$ exec bash -il
(sci) ashee-m1-515:~ amitava$

@jade-tea-h
Copy link

.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
Copy link

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
Copy link

@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
Copy link

voioo commented Apr 22, 2024

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

@jcayzac
Copy link

jcayzac commented Jun 11, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants