Score:0

Opening gnome-terminal and running a command that uses environmental variables from .bashrc

kr flag

I am making a .sh file that would be run by Right click -> Run as program by non-power-users. It should open a terminal window and run a docker run command, that uses some environmental variables.

My issue is, I am not able to tell the terminal to load these environmental variables.

I have export VARIABLE=value in .bashrc and tried running

gnome-terminal -- /bin/bash -c "source .bashrc && echo \$VARIABLE && echo $VARIABLE"

but it outputs two empty lines. Interestingly, if I run

gnome-terminal -- /bin/bash -c "cat .bashrc"

it shows the content of the intended .bashrc with VARIABLE in it. How can I solve this? I also tried --rcfile ~/.bashrc, no luck.

Score:3
hr flag

The issue here is that bash -c opens a non-interactive shell, and (at least in the case of the default Ubuntu ~/.bashrc, as copied from /etc/skel on account creation), there is this close to the top:

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

So unless you added and exported your VARIABLE before that, it will simply be skipped. You could do either

gnome-terminal -- /bin/bash -ic "source ~/.bashrc && echo \"\$VARIABLE\"; read"

or (simplifying the quoting)

gnome-terminal -- /bin/bash -ic 'source ~/.bashrc && echo "$VARIABLE"; read'

(I added the read to prevent the terminal from exiting with the shell, since my gnome-terminal profile does that by default).

Rolf avatar
kr flag
Meanwhile I have also figured this out, thank you, this is the explanation of what's happening! I ended up adding `-i` after fixing the quotation issues, and it works now.
Score:2
uz flag

Programs started from the graphical environment don't see variables in .bashrc. Create [some file name].sh in /etc/profile.d instead and set those variables there.

Rolf avatar
kr flag
Do you suggest copying the variables to a separate file in `/etc/profile.d`? Other thing: it seems like they don't even see environmental variables even if they are set, e.g. `gnome-terminal -- bash -c "export VARIABLE=value; echo $VARIABLE;"` doesn't print anything. Is there a reason for this?
Rolf avatar
kr flag
I just realised I should use `'` instead of `"` or use `\$` inside `"` to be taken literally, so please ignore the second sentence.
Gunnar Hjalmarsson avatar
uz flag
@Rolf: Yes, that's my suggestion. Maybe worth a try.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.