sxhkd icon indicating copy to clipboard operation
sxhkd copied to clipboard

Exported environment variables of .bashrc are not visible

Open florian-pe opened this issue 4 years ago • 3 comments

Hi, I'm using sxhkd to launch a perl script. And this perl script uses an environment variable (PERL5LIB) to get the paths of my personal perl modules.

I just found out that when I run this script via sxhkd, I don't see the enironment variables that I've exported in .bashrc.

I have a workaroud for my particular problem. But is there a way to execute commands in .sxhkdrc with the environment variables defined in .bashrc or not ?

If not, wouldn't it be desirable ?

florian-pe avatar Feb 23 '22 09:02 florian-pe

@florian-pe why not just import your varriables from your .bashrc?

etherbiswas avatar Mar 20 '22 16:03 etherbiswas

@Ahanaf-Ether Of course I'm exporting PERL5LIB in my .bashrc and it's works fine in the terminal and in my perl scripts and modules. But it doesn't work when I write a perl one-liner into sxhkdrc. Because every environment variables that I set in my bashrc is invisible to commands written in sxhkdrc and executed by sxhkd.

florian-pe avatar Mar 21 '22 14:03 florian-pe

~/.bashrc is only read from bash, if it is an interactive shell, see here. sxhkd does not execute commands in an interactive shell. That would be a big mistake.

You should put environment variables, which are not only required by your interactive shell, but by your system in general in your ~/.profile or even in /etc/profile.d.

Bash has a feature, that if a shell is used to execute a program (bash -c '...'), it sources the file specified in the environment variable BASH_ENV before executing the command. So you could define that environment variable inside your ~/.profile and point it to your ~/.bashrc, which should make bash source the ~/.bashrc, everytime a new shell is created to execute a command. But I would advice you against that.

Or, something a bit better: Create a new file ~/.bashenv, specify all environment variables in that one, source ~/.bashenv inside your ~/.bashrc and point BASH_ENV to it inside your ~/.profile.

Use the ~/.bashrc for what it is supposed to be used: additional configuration for an interactive shell, like your prompt, configuring the bash history, or other bash options, which you only want to have in an interactive shell. Since you might have certain shell features enabled only if the shell is interactive, some people put the following in the beginning of their ~/.bashrc (to be extra careful):

[[ $- != *i* ]] && return

This basically says: Stop interpreting this file, if the shell is not interactive. Like I said, this should not be required, since bash will only read ~/.bashrc if the shell is interactive in the first place. But there might be a wrong source command in some other file for the ~/.bashrc.

I would advise you to put environment variables, that are not only relevant to interactive shells into your ~/.profile.

mychris avatar Mar 27 '22 10:03 mychris