3

So my .profile looks like this:

if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi

if [ -f "$HOME/.local/share/profile" ]; then . "$HOME/.local/share/profile" fi

set PATH so it includes user's private bin directories

PATH="$HOME/bin:$HOME/.local/bin:$PATH" export VISUAL=nano export EDITOR="$VISUAL" stty -ixon

function test-func { echo test-func }

alias test-alias='echo test-alias'

When I log into Gnome (on Fedora 35) and open the terminal emulator, I can't execute the function test-func.

$ test-func
bash: test-func: command not found

But when I put the function in my .bashrc file and re-logout and log back in, I am able to execute the test-func function.

Is there a reason why functions added to .profile are not available in the environment?

Edit: So I noticed that when I log in using a non-graphical login (using the Ctrl + Alt + F3 trick), the test-func function is available. Is there a reason why Gnome doesn't make functions in the .profile available to non-login shells?

Edit 2: I also noticed that using a POSIX-compliant syntax for the functions (func-name () { ... } instead of function func-name { ... }) had no effect.

wheeler
  • 304
  • See https://unix.stackexchange.com/questions/88106/why-doesnt-my-bash-profile-work – thanasisp May 11 '22 at 23:14
  • What login manager are you using? GDM? – frabjous May 11 '22 at 23:17
  • @frabjous whatever the default one that is included with Fedora 35 – wheeler May 11 '22 at 23:18
  • @thanasisp That answer doesn't explain why functions in .profile aren't loaded in Gnome. I know the .profile is being sourced when I log in, because I can put environment variables in there and they are available in my terminal emulator as well as applications launched directly from Gnome. – wheeler May 11 '22 at 23:30
  • I believe it is because on Fedora and other modern systemd-logind based login managers, the graphical login is mediated through the systemd --user daemon. It inherits the environment variables of the login shell that launched it, but it directly executes the processes that start your UI. Your gnome-terminal is a child of that process. As far as the shell in your terminal can tell, it has the inherited environment variables but no functions. – jsbillings May 11 '22 at 23:58
  • I wonder if the fact that function test-func { ... } isn't POSIX compatible is a factor here? Does it behave differently with test-func () { ... } ? – steeldriver May 12 '22 at 00:29
  • @wheeler I meant this answer of this post, the links to wikis could be useful for other major distributions, but I see you updated with Fedora. Perhaps you could find something similar into your distribution's documentation. – thanasisp May 12 '22 at 00:55
  • Do you have a ~/.bash_profile file? Are other things set in your .profile working as expected? The variables, for example? – terdon May 12 '22 at 11:54
  • @steeldriver I updated the question to reflect trying to use a POSIX-compliant syntax, it had no effect. – wheeler May 12 '22 at 16:49
  • @terdon No ~/.bash_profile exists, and the .profile is otherwise working as expected because the environment variables defined in there are available in the terminal emulator after login. Additionally, if I add touch ~/profile-test-file to my ~/.profile, the file gets created when I log in. When I add touch ~/bashrc-test-file to my ~/.bashrc file, the file gets created when I start the terminal emulator. – wheeler May 12 '22 at 16:52
  • What does ls -l /bin/sh give? And grep $(id -un) /etc/passwd | cut -d: -f7? Also, you can list all the environment variables and functions in bash by calling the builtin set without parameters (set | less to page it). – Vilinkameni May 18 '22 at 11:24
  • After checking in QEMU, Fedora seems to have /bin/sh set to bash. Though it doesn't explain the issue in that case, as Bash doesn't sanitize variables even when executed as /bin/sh, this answer offers a possible workaround, which is to set the shell in GNOME Terminal as a login shell: https://unix.stackexchange.com/a/505397/367454 Do note that the default /etc/bashrc and /etc/profile in Fedora in the comments contain a mention of which of those files should specifically hold functions. – Vilinkameni May 18 '22 at 13:18

1 Answers1

1

This might be more like a comment, but since I don't have enough reputation, here are what I've got:

  • This issue is not specifically related to Gnome, I'm on KDE and I'm having it too;
  • If I put the function into ~/.bash_profile, it will be available. Could you confirm the same? If it also happens to you that way, then we have another clue: Fedora treats ~/.profile and ~/.bash_profile differently regarding non-login shells.

It actually treats them differently in regard to login shells: if you login with X, ~/.profile gets sourced first, even before ~/.bash_profile, but when there's no GUI, only ~/.bash_profile is sourced (as the manual describes). You can run journalctl to see that /etc/X11/xinit/Xsession inits the GUI session, and that Xsession script sources /etc/X11/xinit/xinitrc-common which seems to exist only on Fedora and which in turn sources ~/.profile.

My two cents.