LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 03-23-2012, 09:14 AM   #1
Iskendar
Member
 
Registered: Jul 2003
Posts: 30

Rep: Reputation: 15
KDE Menu launches bash scripts without environment variables


Hi all,

Trying to set up a little OpenCV demo for our open door at work, so I wanted to launch a bunch of scripts from the desktop. Tried adding them to the start menu, didn't work. Set 'Run in terminal' with the option --noclose, and then I saw that obviously some paths were missing. Apparently the shell starts without running .bashrc, so none of the environment variables are set (checked this). Clicking the script in dolphin or a folder view: same problem.
If I just start up a terminal and run the script from the command line, it works no problem. What am I missing here?

Running Kubuntu 11.10 with kde 4.7.
 
Old 03-24-2012, 02:09 AM   #2
Slackyman
Member
 
Registered: Mar 2011
Location: Rome - Italy
Distribution: Slackware 13.1
Posts: 347

Rep: Reputation: 44
bashrc and bash_profile scripts are loaded only in login shells.
If you are using Konsole keep in mind that it's NOT a login shell, so it doesn't load scripts (~/.bashrc /etc/profile and /etc/profile.d/*).
Try addin --ls as additional option.
If you are using another shell emulator try checking its options to verify which one enables a login shell behaviour.
 
Old 03-24-2012, 11:09 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
That's not quite correct, at least on my distro.

bash_profile is loaded with login shells, and bashrc with non-login shells. The INVOCATION section of your bash man page will detail which files are called in which situations on your system.


But the real problem here is that guis don't launch programs and scripts from a "shell", they call on your system's execution routines directly. Or something like that. The important thing is that it's not done in the context of a shell environment.

You generally need to ensure that all needed variables are set within the script itself, or else run the script with a terminal program, with the desired environment set at the beginning. One way to to that is to prefix your command with the setting (variables set immediately before a command are placed in that command's environment).

I just did this as an example. I wrote a quick script:

Code:
#!/bin/bash

echo "[$foo]"

exit 0
Then set up my menu button command like this:

Code:
foo='hi there' konsole --noclose -e '/home/david/test/script.sh'
When I ran it, a konsole window opened up, with "[hi there]" printed in it.


A final option may be to use your /etc/environment file to designate system-wide environment variables. (Note that this file isn't a full shell file, and can only contain "name=value" pairs.) I wouldn't recommend this for trivial settings though.
 
1 members found this post helpful.
Old 03-24-2012, 12:28 PM   #4
Iskendar
Member
 
Registered: Jul 2003
Posts: 30

Original Poster
Rep: Reputation: 15
Thanks. That is the clearest explanation of what is going on I've found anywhere.
If this were facebook, I'd press "like".
 
Old 03-25-2012, 09:54 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Glad to be of service.

As for "liking" it, that's what the "Did you find this post helpful?" link is for, as per my signature.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] using environment variables in scripts politik Programming 16 06-24-2010 10:40 PM
Importing environment variables into bash scripts JDska55 Linux - Newbie 1 08-11-2009 04:27 PM
Scripts & Environment Variables geeman2.0 Linux - General 12 07-05-2005 05:10 PM
Why can't my scripts set environment variables? Matthew3 Linux - Software 5 08-28-2004 05:26 PM
Environment Variables in Scripts louisb Linux - General 3 01-15-2004 04:56 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 06:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration