Setting environment variables

Questions about applications and software
Forum rules
Before you post please read how to get help
mikeglaz
Level 2
Level 2
Posts: 77
Joined: Tue Feb 28, 2012 10:32 pm

Setting environment variables

Postby mikeglaz » Sun Sep 22, 2013 5:57 pm

I need to set some environment variables for my Ruby on Rails project.

I have the following in ~/.bashrc
export GMAIL_USERNAME="username@gmail.com"
export GMAIL_PASSWORD="password"

but then when I do echo $GMAIL_USERNAME a blank line is returned.

User avatar
karlchen
Level 17
Level 17
Posts: 7950
Joined: Sat Dec 31, 2011 7:21 am
Location: Germany

Re: Setting environment variables

Postby karlchen » Sun Sep 22, 2013 7:56 pm

Hello, mikeglaz.

I added the 2 lines

Code: Select all

export GMAIL_USERNAME="username@gmail.com"
export GMAIL_PASSWORD="password"
at then end of my personal .bashrc file.
Next I executed the command

Code: Select all

bash
in order to launch a new instance of bash.

Code: Select all

echo $GMAIL_USERNAME
displayed "username@gmail.com" without the double quotes.
.bashrc is only read automatically when you launch a new instance of bash. This will happen e.g. when you open a new terminal window.

HTH,
Karl
Image
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

mikeglaz
Level 2
Level 2
Posts: 77
Joined: Tue Feb 28, 2012 10:32 pm

Re: Setting environment variables

Postby mikeglaz » Tue Oct 01, 2013 7:08 pm

hmm, this doesn't work when I open a new terminal window. It only works after I run 'bash'. I need these environment variables to be present when I run my Ruby on Rails project.

User avatar
karlchen
Level 17
Level 17
Posts: 7950
Joined: Sat Dec 31, 2011 7:21 am
Location: Germany

Re: Setting environment variables

Postby karlchen » Wed Oct 02, 2013 7:08 am

mikeglaz wrote:hmm, this doesn't work when I open a new terminal window. It only works after I run 'bash'.
Could it be that the shell which will be used by your terminal programme is not bash? In order to find out, simply open a terminal window and execute

Code: Select all

echo $SHELL
In case the returned string is not /bin/bash, then your shell is not bash and this is the reason why .bashrc will be ignored when opening a terminal window. The solution in this case would be adding the needed variables to the config file which will be used by that shell programme.

Karl
Image
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

Habitual
Level 13
Level 13
Posts: 4866
Joined: Sun Nov 21, 2010 8:31 pm
Location: 0.0.0.0

Re: Setting environment variables

Postby Habitual » Wed Oct 02, 2013 9:32 am

mikeglaz wrote:hmm, this doesn't work when I open a new terminal window.


after:

Code: Select all

export GMAIL_USERNAME="username@gmail.com"
export GMAIL_PASSWORD="password"


Code: Select all

source ~/.bashrc

Now available to current session and all other sessions.
Image

michael.purwoadi
Level 1
Level 1
Posts: 2
Joined: Fri Oct 04, 2013 8:23 pm

Re: Setting environment variables

Postby michael.purwoadi » Fri Oct 04, 2013 9:09 pm

Hi, sorry I'm new in Linux Mint (Maya) - cinnemon, before I used for years Mandriva 2010.0.
I would like to set environment variables when launching an application from menu or desktop.
Following instruction in https://help.ubuntu.com/community/Envir ... t_variable,
I didn't see how to do it in cinnemon through main menu.
Any help is welcome. Regards.

User avatar
karlchen
Level 17
Level 17
Posts: 7950
Joined: Sat Dec 31, 2011 7:21 am
Location: Germany

Re: Setting environment variables

Postby karlchen » Sat Oct 05, 2013 7:55 am

Hello, michael.purwoadi.

Minor correction first: The desktop environment is spelt Cinnamon with an a, not with an e. :wink:

To your question:

Right-click on the [Menu] button.
From the context menu select "Edit menu". This will launch cinnamon-menu-editor.
Navigate to the sub-menu folder which holds the programme item that you want to change.
Select the programme item and edit the commandline which will be executed.
How you can set a variable for a specific programme only can be seen in the instruction which you linked to.
So what you would enter in the commandline field would be:

Code: Select all

env APPMENU_DISPLAY_BOTH=1 digikam -caption "%c" %i
sticking with the given example.
The relevant part is the command env. env can and will be used to export a variable (APPMENU_DISPLAY_BOTH=1) and then launch the rest of the commandline as it is (digikam -caption "%c" %i).
I.e. the commandline part that launches digikam in the given example will not be interpreted by env, but it will simply be launched.

HTH,
Karl
Image
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

michael.purwoadi
Level 1
Level 1
Posts: 2
Joined: Fri Oct 04, 2013 8:23 pm

Re: Setting environment variables

Postby michael.purwoadi » Sat Oct 05, 2013 6:11 pm

It is working in Cinnamon. Thank you Karl.

User avatar
karlchen
Level 17
Level 17
Posts: 7950
Joined: Sat Dec 31, 2011 7:21 am
Location: Germany

Re: Setting environment variables

Postby karlchen » Sat Oct 05, 2013 8:20 pm

Great! You're welcome. :-)
Image
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

mikeglaz
Level 2
Level 2
Posts: 77
Joined: Tue Feb 28, 2012 10:32 pm

Re: Setting environment variables

Postby mikeglaz » Sat Oct 12, 2013 4:46 pm

Is there a way I can have

Code: Select all

source ~/.bashrc
run every time I open a terminal window?

User avatar
karlchen
Level 17
Level 17
Posts: 7950
Joined: Sat Dec 31, 2011 7:21 am
Location: Germany

Re: Setting environment variables

Postby karlchen » Sat Oct 12, 2013 4:56 pm

Hello, mikeglaz.

This should be the default behaviour whenever you open a new terminal window, because each new terminal window will spawn a new instance of bash. And ~/.bashrc should be read (sourced) whenever a new instance of bash is launched. - It works this way here. - I will have to consult the bash manpages to find out whether there is a way of making bash not read .bashrc when it is started, because if we know how to prevent this from happening, it should be clear how to get back the default behaviour.
OK. This was easy. :-)

Code: Select all

man bash
explains at minimum 2 ways of preventing bash from reading its ~/.bashrc file:
  • Invoke bash using its second name sh. I.e.

    Code: Select all

    bash
    will allow bash to read ~/.bashrc.

    Code: Select all

    sh
    will launch bash, but ~/.bashrc will not be read. (1)
  • Invoking bash with the commandline argument --norc will prevent bash from reading ~/.bashrc, too.

    Code: Select all

    bash --norc
Now the question is how exactly do you launch a new terminal?
Then it will be necessary to check which commandline will be executed to open the terminal window.
Next it will be necessary to find out whether e.g. the terminal programme launches sh instead of bash or whether it passes the --norc argument to the shell.
Finally it will be necessary to make sure that the terminal programme launches bash without telling it to ignore ~/.bashrc.

So here is the question #1 to you: How exactly do you launch a new terminal window?
So here is the question #2 to you: What is the exact commandline which your action will trigger?


Kind regards,
Karl
--
(1)
In fact, on Ubuntu and Mint, bash can only be invoked as bash, not as sh by default.
Reason:
Invoking sh will launch dash, not bash, because /bin/sh is a symbolic link to /bin/dash by default. - And, of course, by default /bin/dsh does not read ~/.bashrc.
Image
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

mikeglaz
Level 2
Level 2
Posts: 77
Joined: Tue Feb 28, 2012 10:32 pm

Re: Setting environment variables

Postby mikeglaz » Wed Oct 16, 2013 1:45 pm

Karl,

I start a new terminal window with gnome-terminal.

I did some googling for loading .bashrc and found an Ubuntu page that told me to include source ~/.bashrc inside my .bash_profile. This has worked and my environment variables are loading. Is this a good solution?

mike

Habitual
Level 13
Level 13
Posts: 4866
Joined: Sun Nov 21, 2010 8:31 pm
Location: 0.0.0.0

Re: Setting environment variables

Postby Habitual » Wed Oct 16, 2013 6:42 pm

mikeglaz wrote:Is there a way I can have

Code: Select all

source ~/.bashrc
run every time I open a terminal window?

Not necessary. it is just for the current session (so you don't have to logout|in)

Closing all bash-enabled terminals and restarting terminal does the same thing.
Image


Return to “Software & Applications”

Who is online

Users browsing this forum: No registered users and 16 guests