You are not logged in.

#1 2020-10-04 04:08:04

duyinthee
Member
Registered: 2015-06-14
Posts: 242
Website

update bash or source .bashrc for non-interactive shell

For Example, I usually change environment settings in ~/.bashrc file for nnn file manager here and there. Then, do

$ source ~/.bash_profile

It effects if I run "nnn" in interactive shell. But if I run "nnn" via keybinding or script using

st -e nnn

for instance, it does not effect.

I googled and read a lot post online. As far as I understand, non-interactive shell does not source ~/.bashrc.
So, one solution I found is forcing it to source ~/.bashrc every time, like this, for instance,

st -e $SHELL -ic nnn

But what I am looking for is any way to update or source ~/.bashrc for non-interactive only one time without reboot the machine.
I found a post here, but I do

$ exec $SHELL

and it does not work.

So, is there any way to do for non-interactive shell as doing

$ source ~/.bash_profile

for interactive shell.

Last edited by duyinthee (2020-10-04 05:56:54)

Offline

#2 2020-10-04 04:15:01

GaKu999
Member
From: US/Eastern
Registered: 2020-06-21
Posts: 696

Re: update bash or source .bashrc for non-interactive shell

Set BASH_ENV to point to your .bashrc.
That’s it.

export BASH_ENV=“$HOME/.bashrc”

Or any other that you want.

You can also export the environment variables for nnn that you change, but if you change them too frequently then use bashrc.

When you find something stable that you like then port the nnn environment variables to your .bash_profile

Edit: not efficient, see @seth suggestion and use env instead.

Last edited by GaKu999 (2020-10-04 07:31:27)


My reposSome snippets

Heisenberg might have been here.

Offline

#3 2020-10-04 06:00:37

duyinthee
Member
Registered: 2015-06-14
Posts: 242
Website

Re: update bash or source .bashrc for non-interactive shell

GaKu999 wrote:

Set BASH_ENV to point to your .bashrc.
That’s it.

export BASH_ENV=“$HOME/.bashrc”

Or any other that you want.

You can also export the environment variables for nnn that you change, but if you change them too frequently then use bashrc.

When you find something stable that you like then port the nnn environment variables to your .bash_profile

I have tried,

export BASH_ENV="$HOME/.bashrc"

does not work.

Porting my environment settings to ~/.bash_profile does not work too.

Offline

#4 2020-10-04 06:04:24

GaKu999
Member
From: US/Eastern
Registered: 2020-06-21
Posts: 696

Re: update bash or source .bashrc for non-interactive shell

Have you logged in and out?
It must be set on login by .bash_profile, otherwise the subshells won’t inherit it...

I see a source of problems, because if you are using a DM, IIRC it skips that...

If that’s the case you might have to use .pam_environment, but beware, it haves a different syntax.

And of course, only logout and login is needed, idk who told you that you had to reboot to update environment variables...

Edit: not efficient for what you are doing, follow @seth suggestion and use env instead

Wiki page:
https://wiki.archlinux.org/index.php/En … _variables

Last edited by GaKu999 (2020-10-04 07:34:03)


My reposSome snippets

Heisenberg might have been here.

Offline

#5 2020-10-04 07:01:44

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,476

Re: update bash or source .bashrc for non-interactive shell

Forget the entire approach and check "man env".

Offline

#6 2020-10-04 07:30:00

GaKu999
Member
From: US/Eastern
Registered: 2020-06-21
Posts: 696

Re: update bash or source .bashrc for non-interactive shell

seth wrote:

Forget the entire approach and check "man env".

That is indeed a lot more efficient for what OP needs.

Last edited by GaKu999 (2020-10-04 07:34:49)


My reposSome snippets

Heisenberg might have been here.

Offline

#7 2020-10-04 10:03:26

duyinthee
Member
Registered: 2015-06-14
Posts: 242
Website

Re: update bash or source .bashrc for non-interactive shell

Yeah, env is helpful.
I found the way to what I want, but it is not an answer to my question regarding with bash.
I create a script called .nnn-launcher lets say, as follow;

#!/usr/bin/env sh
env \
NNN_TRASH=1 \
NNN_BMS='d:~/Documents/mshDic;h:~;j:~/Downloads;u:~/Documents' \
NNN_PLUG='m:ow-mplayer;i:pv-sxiv;c:m-convert;a:mp3-edit' \
"$@"

for instance.

then I open nnn in st from keybinding or script like this;

st -e ~/.nnn-launcher nnn

Actually that's exactly what I want. I can edit plugins and bookmarks in the ~/.nnn-launcher at any time without head ache to re-source bash and logout-login again.

But I still want to know

duyinthee wrote:

So, is there any way to do for non-interactive shell as doing

$ source ~/.bash_profile

for interactive shell.

Offline

#8 2020-10-04 10:27:20

GaKu999
Member
From: US/Eastern
Registered: 2020-06-21
Posts: 696

Re: update bash or source .bashrc for non-interactive shell

For non interactive bash init load, use BASH_ENV as said.

Let’s test just in case...

In $HOME/foo

echo bar

Then run:

BASH_ENV=“$HOME/foo” bash -c “echo baz”

It should have bar and baz as output.

Do note that when called as sh it will not load BASH_ENV, for sh IIRC you need to use ENV instead as for bash with —posix.

That includes any script with a sh shebang

By default sh is a symlink to bash, but bash behaves differently when called as sh.

Last edited by GaKu999 (2020-10-04 10:46:28)


My reposSome snippets

Heisenberg might have been here.

Offline

#9 2020-10-04 11:22:15

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,476

Re: update bash or source .bashrc for non-interactive shell

You can also "/bin/bash -i" to explicitly run it as interactive shell and in general set a variable in your bashrc (or any include) that you can test to condition the source.

Offline

Board footer

Powered by FluxBB