2

I have a desktop entry pointing to IntelliJ IDEA IDE. I wanted to add . ~/.bashrc at the beginning of the idea.sh script that is being executed by Desktop Entry. The point of that is that IDEA wasn't picking up my modified SSH_AUTH_SOCK env variable, that was defined there. So I added a very simple . ~/.bashrc at the beginning of idea.sh, to discover that the sourced code is not being run. Now some details.

In ~/.bashrc I have this:

# Set GPG TTY
export GPG_TTY=$(tty)

unset SSH_AGENT_PID
if [ "${gnupg_SSH_AUTH_SOCK_by:-0}" -ne $$ ]; then
  export SSH_AUTH_SOCK="$(gpgconf --list-dirs agent-ssh-socket)"
fi

# Refresh gpg-agent tty in case user switches into an X session
gpg-connect-agent updatestartuptty /bye >/dev/null

I wanted this to run at the beginning of idea.sh, but . ~/.bashrc doesn't work, HOWEVER if I put this exact code directly in the idea.sh, in the same place, it will work just fine.

Sourcing also works if I run idea.sh directly in my terminal. It doesn't work only if I run it by using Desktop Entry. The desktop entry in question is /home/luken/.local/share/applications/jetbrains-idea.desktop:

[Desktop Entry]
Version=1.0
Type=Application
Name=IntelliJ IDEA Ultimate Edition
Icon=/home/luken/Programy/idea-IU-182.4505.22/bin/idea.svg
Exec="/home/luken/Programy/idea-IU-182.4505.22/bin/idea.sh" %f
Comment=Capable and Ergonomic IDE for JVM
Categories=Development;IDE;
Terminal=false
StartupWMClass=jetbrains-idea

So to sum this up:

Running idea.sh in terminal:

#!/bin/sh
#
# ---------------------------------------------------------------------
# IntelliJ IDEA startup script.
# ---------------------------------------------------------------------
#

. ~/.bashrc
echo "${SSH_AUTH_SOCK}" > ~/auth.txt

# ...

Content of auth.txt: /run/user/1000/gnupg/S.gpg-agent.ssh CORRECT.

Running idea.shby using Desktop Entry shortcut:

#!/bin/sh
#
# ---------------------------------------------------------------------
# IntelliJ IDEA startup script.
# ---------------------------------------------------------------------
#

. ~/.bashrc
echo "${SSH_AUTH_SOCK}" > ~/auth.txt

# ...

Content of auth.txt: /run/user/1000/keyring/ssh INCORRECT.

#!/bin/sh
#
# ---------------------------------------------------------------------
# IntelliJ IDEA startup script.
# ---------------------------------------------------------------------
#

# Set GPG TTY
export GPG_TTY=$(tty)

unset SSH_AGENT_PID
if [ "${gnupg_SSH_AUTH_SOCK_by:-0}" -ne $$ ]; then
  export SSH_AUTH_SOCK="$(gpgconf --list-dirs agent-ssh-socket)"
fi

# Refresh gpg-agent tty in case user switches into an X session
gpg-connect-agent updatestartuptty /bye >/dev/null

echo "${SSH_AUTH_SOCK}" > ~/auth.txt

# ...

Content of auth.txt: /run/user/1000/gnupg/S.gpg-agent.ssh CORRECT.

Anyone have any idea what is going on here or how to debug this?

  • Exec doesn't execute more than one binary. If you want to load environment variables, you need to specify them inline in the way of VAR="variable" executable. – Braiam Jan 5 at 13:40
  • But it's executing a single script. source is part of this script. – Łukasz Zaroda Jan 5 at 13:46
  • @steeldriver Bingo! I cannot belive I missed it (the early return). Post this as an answer. :) – Łukasz Zaroda Jan 5 at 15:49
3

Your ~/.bashrc file likely checks whether the shell that it is being sourced into is interactive or not, and in the latter case is returning before completing the instructions in question.

For example, the default ~/.bashrc in Ubuntu systems has the following code near the top:

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

Also be aware that on some systems, /bin/sh may be a shell other than bash (for example, the dash shell) which may not support all of the features of your ~/.bashrc file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.