Can I export PATH twice in .bashrc?
Asked Answered
O

1

8

I'm running Ubuntu 12.04 and trying to be ale to use both the Heroku CLI as well as the newly-downloaded AWS Elastic Beanstalk CLI tools.

I have this code in my .bashrc already:

### Added by the Heroku Toolbelt
export PATH="/usr/local/heroku/bin:$PATH"

And I also want to export this:

#for Elastic Beanstalk - madebyian
export PATH=$PATH:/opt/AWS-ElasticBeanstalk-CLI-2.6.0/eb/linux/python3/

How do I get both CLI tools and/or what's wrong with my syntax EDIT:[if anything]?

Oisin answered 20/3, 2014 at 1:35 Comment(2)
FYI, .bashrc isn't generally the right place to update your PATH -- you'll get entries added multiple times as shells started by other shells invoke it after it's already been run by a parent. Environment updates generally belong in .bash_profile, so they're run only once per login.Pace
Anyhow, "what's wrong?" assumes that anything's wrong. Show us the PATH you're getting, so we can tell why you think anything is wrong.Pace
M
18

You can export it as many times as you want, it will make no difference (after the first export, obviously).

All export does in this context (other than changing the variable itself due to the =) is mark a variable so that it's exported to the environment of future commands.

You can mark it so as much as you want. The effect of the two commands:

export PATH="/usr/local/heroku/bin:$PATH"
export PATH=$PATH:/opt/AWS-ElasticBeanstalk-CLI-2.6.0/eb/linux/python3/

will be to mark PATH as an export variable (it probably is so already since you generally want your path to be inherited) and set it to:

/usr/local/heroku/bin:$PATH:/opt/AWS-ElasticBeanstalk-CLI-2.6.0/eb/linux/python3/

where $PATH was the path before executing those commands.

The only thing you need to be careful of is the ordering. For example, if /usr/local/heroku/bin contains an executable program called ls, that's probably going to make life difficult for you if you're trying to get a directory listing. For that reason, I tend to add directories only to the end of the path.

Alternatively, you can make the order less permanent by providing an alias or function which changes your path to the Heroku-preferred one only for the current session.


Keep in mind that the files that get run by bash are a complex matter. .bashrc is run for interactive, non-login shells so may not run in all cases (I, for one, fix that by calling .bashrc at the end of my .bash_profile though some people may cringe at that).

You're probably better off setting (and exporting) the path in your .bash_profile.


And, if there is something wrong with what you're doing (your syntax seems fine but there may be other problems we cannot discern due to lack of information), you should perform the following steps:

  • echo $PATH before and after each export command to see if something is stuffing it up.
  • ensure that each component in the path actually exists (i.e., that you have the correct directories in there).
Mossbunker answered 20/3, 2014 at 1:38 Comment(2)
Maybe it will be usefull to make a ref to #415903Valenti
+1; > "exported to the environment of future commands" - more accurately, exported to the environment of any future child processes of the current (shell) process. This implies that preexisting shell instances and their (potentially future) child processes are NOT affected. Thus, as @CharlesDuffy points out, ~/.bash_profile is the best place to put export commands.Kush

© 2022 - 2024 — McMap. All rights reserved.