.Bashrc Not Read When Shell Script Is Invoked from Desktop Shortcut

Exported variable in bashrc file not visible when running as another user

Your problem is that .bashrc is configure explicitly to only run for interactive shells. Look at the top of /home/foouser/.bashrc:

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

If you add a set -x to your /tmp/bootstrap.sh script, you will see when you execute it the following:

+ echo 'export FOO=foo'
+ . /home/foouser/.bashrc
++ case $- in
++ return
+ echo 'sourced foouser bashrc'
sourced foouser bashrc
+ set
+ grep FOO

There you can see it hits the return command in the case statement.

You can force an interactive shell with the -i option:

root@ed3085a447ad:/# su - foouser -c 'bash -i -c /tmp/bootstrap.sh'
bash: cannot set terminal process group (-1): Inappropriate ioctl for device
bash: no job control in this shell
sourced foouser bashrc
FOO=foo

Node command not found when bash script is run through desktop icon

A workmate solved the problem. Just add

PATH="/home/myuser/.nvm/versions/node/v12.14.1/bin:$PATH"

after the #!/bin/bash line.

So the bash script is finally:

#!/bin/bash
PATH="/home/myuser/.nvm/versions/node/v12.14.1/bin:$PATH"

cd /home/myuser/app/todo_app/
node ./app.js
echo "Bye"
$SHELL

Add input and show output in terminal from .desktop file

Got it working, First install lxterminal and then edit Openconnect.desktop file.

[Desktop Entry]
Version=1.0
Name=Openconnect
Comment=This is my VPN Connection
Exec=lxterminal --command="/home/support/Documents/Openconnect.sh"
Icon=/home/support/Documents/openvpn.png
Terminal=false
Type=Application
Categories=Utility;Application;

how to make functions visible to bash scripts?

/etc/bashrc, ~/.bashrc are not read when not running in an interactive mode.

You might see something similar to

case $- in
*i*) ;;
*) return;;
esac

or

[ -z "$PS1" ] && return

in your ~/.bashrc.

Consider adding your function to ~/.profile or to ~/.bash_profile (if the latter exists).



Related Topics



Leave a reply



Submit