1

I am testing running Ubuntu 22.04. I don't understand why when opening a gnome-terminal using a .desktop file $HOME/.bashrc is not read although when running the same command in an open terminal it is. To be more specific I have a file /home/rquint/.local/share/applications/Sage.desktop which contains the line

EXEC=gnome-terminal --class=SageTerminal -- /home/rquint/Sage/sage-9.6/sage

and I have the line

export DOT_SAGE=/home/rquint/dotsage

in /home/rquint/.bashrc

If I use the desktop file to start sage, it runs correctly but the value of DOT_SAGE is the sage default /home/rquint/.sage

If I open a terminal (where I can check DOT_SAGE and it is the value as exported in my .bashrc) and execute

gnome-terminal --class=SageTerminal -- /home/rquint/Sage/sage-9.6/sage

sage is started in the terminal and the value of DOT_SAGE is correct.

I had assumed that the terminal started by the Sage.desktop file was a non-login shell, but if it isn't, what is it? Is there a way to pass it the value of the environment variable I want?

(In the past this was unimportant, but with the change of Firefox to a snap, sage's method of displaying plot3d output as an html file in a path that contains a hidden folder doesn't work.)


Thank you @egmont (answer here...) It's a sort of "Yes and No". Your suggestion about using env was very helpful. While searching AskUbuntu I have found an old question and answer desktop file with bashrc environment that also gave me some insight. When I start sage using the desktop file I do get an noninteractive shell, which means I can set my DOT_SAGE variable in $HOME/.profile. sage provides a command line like environment in which you can run shell commands. I can do things like echo $SHELL (mine is bash) and test what sort of shell sage is running in with [ -z "$PS1" ] && echo "Noninteractive" || echo "Interactive" (mine is noninteractive). You can run pwd but you can't use cd.

andrew.46
  • 38,003
  • 27
  • 156
  • 232
  • It is most likely sourcing .bash_profile. Make sure you are sourcing .bashrc from there and you should be good – ElefantPhace May 20 '22 at 01:00
  • I'm running a standard Ubuntu install. AFAIK that means there is only $HOME/.bashrc and no local or global .bash_profile. – Richard Quint May 20 '22 at 02:47
  • Simply echo "$SHELL". Read the man page for your shell - it has a section on "Startup Files" with man $ SHELL or, for the picky man :${SHELL##*/}". – waltinator May 20 '22 at 02:55

1 Answers1

3

I had assumed that the terminal started by the Sage.desktop file was a non-login shell

Terminal and shell are two different things. A terminal cannot be a login shell or a non-login shell. A shell can be a login shell or a non-login shell. A terminal can launch within itself a login shell, a non-login shell, or neither.

When you tell gnome-terminal what command to start, it starts that command instead of your default shell. That is, it does not start any shell.

In your first example there's no bash shell involved at all, the terminal starts Sage directly, without any shell in the game. That's why .bashrc or .bash_profile isn't read.

In your second example you manually start a gnome-terminal with the default shell bash in interactive mode, this sets the desired environment variable, and from here you start another gnome-terminal which inherits these environment variables, that's why they remain set.


One possibility you can do is to write a shell script wrapper to your Sage command, and launch that from gnome-terminal in your .desktop file.

Another possibility is to pass bash -l -c Sage or bash -i -c Sage to your gnome-terminal command line, i.e. explicitly add the shell in between. You need to tell it to act like an interactive or a login shell.

If all you need to do is set a few environment variables, you can use the env command instead of a shell: start gnome-terminal with its executed command being env DOT_SAGE=blahblah Sage. This is probably the simplest if you don't mind having to maintain this environment variable at two different places.

Yet another possibility is to set the desired environment variable in a way that it's already set for every process started by the graphical environment. .bash_profile, .xsession or .xprofile might be the right place for that; alas you inconveniently have to log off from your graphical session and log back in for the change to take effect. (The exact file(s) read upon a graphical login might vary across distributions, graphical environments, method of login, and might have changed over the years, that's why my answer is a little big vague here.)

egmont
  • 8,225