How do I change the order of $PATH? - bash

echo $PATH gives me
/Library/Frameworks/Python.framework/Versions/3.4/bin:/Applications/Sublime Text 2.app/Contents/SharedSupport/bin:/Users/pathreskoo/anaconda/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/opt/local/bin:/opt/local/sbin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin
but when I want to change the order of /usr/local/bin to the front of /Library/Frameworks/Python.framework/Versions/3.4/bin, I type
sudo emacs /etc/paths
I only get
/usr/local/bin:
/usr/bin:
/bin:
/usr/sbin:
/sbin:
How can I insert /usr/local/bin in front of my PATH?

You can set your PATH in the file .bash_profile, which is in your home directory.
More specifically, you can simply add the following line to the end of that file
export PATH=/usr/local/bin:$PATH
This results in /usr/local/bin being prepended to the existing PATH. In other words, the folder /usr/local/bin is inserted in front of your PATH, and so it would have the highest priority. You can also append a folder to your path by doing
export PATH=$PATH:/usr/local/bin
In general, you can set the order of the folders or files that you export in a similar way as the following:
export PATH=/usr/local/bin:/Applications/Sublime Text 2.app/Contents/SharedSupport/bin:/Users/pathreskoo/anaconda/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/opt/local/bin:/opt/local/sbin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin
Note: this is not the only place you can set the PATH, but it is a common one.

Your $PATH normally overridden by the initiation part of your shell. Normally follows the system-wide profile (/etc/profile), then user-side profile (if you use bash .profile, .bash_profile, .bashrc) and any source command in these files. The overridden command mainly in .bashrc
Edit you .bashrc file and find $PATH, you may find the export command and delete the path you do not want. export $PATH=/usr/local/bin:$PATH override the command user-wide.

Related

I set my own path to $PATH variable to have global access to some of my scripts, but I can not access them in any directory

I used ~/.bash_profile to add a new path to my $PATH variable. I added export PATH="/usr/sbin:$PATH" to my ~/.bash_profile, then saved it, and typed source ~/.bash_profile in my terminal. I echo $PATH and I see that I have this path in my $PATH variable. But I cannot access lsof utility (this command is in /usr/sbin directory). Also, I have this situation with any directories. I wanted to add a directory, I added it, I had this path in my $PATH variable, but I could not access scripts that I have there.

Did I mess up my PATH? .bash_profile

I am trying to open up files from my terminal using atom. Which resulted in -bash: atom: command not found. I then followed, installing the shell commands from the editor. That resulted in "EEXIST: file already exists, mkdir '/usr/local/bin'" . I've previously tried to follow other recommendations on how to add the subl alias and I guess I tried way too many things, that I didn't understand. Could I have messed up my $PATH ?
When I echo my path this is what it is /bin:/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin.
My bash_profile looks like this when I opened it with nano:
export PATH=/bin:/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:$PATH
export EDITOR='subl -w'
I don't remember if I added that first line when I tried adding subl.
Should I remove that? I'm also using a MacOS High Sierra.
UPDATE
I removed what I had previously added in my .bash_profile and my path is now: /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin.
I then ran the command: sudo ln -s /Applications/Atom.app/Contents/Resources/app/atom.sh /usr/local/bin/atom but got a ln: /usr/local/bin/atom: No such file or directory error
You can remove the export PATH statement from your .bash_profile; it doesn't add anything that isn't already there. (Except maybe /usr/local/sbin, but I would remove that until you can demonstrate a need for it.)
Initially, PATH will include the directories listed in /etc/path:
% cat /etc/paths
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
Any of your shell configuration files can add to the PATH. Assuming you haven't significantly altered your configuration, /etc/profile will run path_helper to add any directories listed in files under /etc/paths.d.
You only need to update PATH if you need to add a directory that is not already there. If atom is located in one of the above paths, you don't need to do anything. It sounds like you attempted to create /usr/local/bin when it already (as it should) exists, then perhaps did not ultimately create the symlink for atom under /usr/local/bin.
Try running just the ln command from the question you linked (using sudo if necessary), then confirm that /usr/local/bin/atom does indeed exist.

how to change path in OSX maverick?

trying to set my path, when I do
sudo nano /etc/paths
in the terminal I have added a path say /usr/test
after saving this file when I do:
echo $ PATH
the /usr/test path is not included?
If you want to add something to your $PATH you do it in .profile or .bash_profile etc
example

How to add a directory to PATH in the file .bashrc?

After installing python(EPDFee), I want to add the /bin directory to $PATH variable. I am use bash shell. I have found that I have to add the following line in the file .bashrc
export PATH=/home/usrname/epd/bin:$PATH
I have found the file .bashrc, it reads
PATH=$PATH:/opt/bin
# Added by Canopy installer on 2014-03-29
# VIRTUAL_ENV_DISABLE_PROMPT can be set to '' to make bashprompt show that Canopy is active, otherwise 1
VIRTUAL_ENV_DISABLE_PROMPT=1 source /home/an/Enthought/Canopy_64bit/User/bin/activate
Could you please tell me where can I add export PATH=/home/usrname/epd/bin:$PATH to or is should be added in another file?
add the following line to your .bashrc file ...don't forget to replace your path.
export PATH="/path/directory:$PATH"
then do
source .bashrc
to let the changes make effects. I am not sure about other distributions of Linux but It will work on CentOS and RedHat.
You can do it like this :
Define a EPD_HOME var and append it to PATH
EPD_HOME=/home/usrname/epd/bin
PATH=$PATH:$EPD_HOME:$HOME/bin
export PATH
Notice that the $EPD_HOME variable is in the PATH variable and is now loaded once you open a connection to your user on the machine.

Add a relative path to $PATH on fish startup

I want to add ./bin directory (which is relative to current shell directory) to $PATH on fish startup. Note that fish is a shell.
echo $PATH
set PATH ./bin $PATH
echo $PATH
If I place these lines inside ~/.config/fish/config.fish the shell will echo the same collection of paths. Absolute paths are added properly.
If I open the shell and type the same set PATH ./bin $PATH inside some directory containing bin it is added successfully. However when there is no bin inside current directory it shows me an error.
set: Could not add component ./bin to PATH.
set: Value too large to be stored in data type
I'm running fish 1.23.1 on OS X Lion.
The best way I have found to persistently add a path to your $PATH is
set -U fish_user_paths $fish_user_paths ~/path/name
This prepends to $PATH. And since it's persistent, the path stays in $PATH on shell restarts.
It's more efficient than putting a command in your config.fish to modify your $PATH, because it only runs once compared to running on every shell restart.
The variable fish_user_paths is intended to be set by the user1, as stated by ridiculousfish, the maintainer of fish.
Consider creating a fish function for convenience: 2
# ~/.config/fish/functions/add_to_path.fish
function add_to_path --description 'Persistently prepends paths to your PATH'
set --universal fish_user_paths $fish_user_paths $argv
end
And use it as:
$ add_to_path foo bar # Adds foo/ and bar/ to your PATH
Notes
On that page the author gives the example set -U fish_user_paths ~/bin. This overwrites fish_user_paths with a single value of ~/bin. To avoid losing existing paths set in fish_user_paths, be sure to include $fish_user_paths in addition to any new paths being added (as seen in my answer).
My dotfiles contain a slightly more advanced version that skips adding duplicates https://github.com/dideler/dotfiles/blob/master/.config/fish/functions/add_to_user_path.fish
I'd never heard of fish before this. I just installed it so I could try it out (and deleted a few paragraphs I had written here before realizing that fish is a shell).
It looks like set PATH dir-name $PATH is the right syntax to prepend a directory to $PATH.
But adding a relative directory name to $PATH is almost certainly a bad idea, and your shell is doing you a favor by warning you when the directory doesn't exist. (fish is designed to be user-friendly.)
Use an absolute path instead:
set PATH $PWD/bin $PATH
and first check whether $PWD/bin exists, printing an error message if it doesn't.
As for the "set: Value too large to be stored in data type" message, could you be adding the directory to your $PATH multiple times? There should be some way to check whether a directory is already in $PATH before adding it.
I think the answer is that using set -U is a red herring. Instead, add the following to ~/.config/fish/config.fish:
if status --is-interactive
set PATH $PATH ~/.local/bin;
end
direnv http://direnv.net/ is a good utility to help with what you're doing.
Generally, prepending $PATH with ./bin is insecure, as anyone with write-access to a shared directory could hide malicious code in e.g. ./bin/ls. That code would execute when you run ls in the shared directory.
direnv does not solve this problem (it works based on .envrc files, but anyone could be placing those), but at the very least it makes you aware when you cd into a directory that $PATH is getting modified:
$ cd my_project
direnv: loading .envrc
direnv export: ~PATH
It seems like fish won't add a non-existing directory path to PATH. That applies to relative paths too. But if you create bin directory in your home directory set PATH ./bin $PATH will work properly on each startup since it is executed from home. This is kind of a hack though.
Personally, I think there is only a small risk in adding . to the $PATH, so long as it's the last item, because a rogue ls (or whatever) in the CWD will not be found before /usr/bin/ls.
I add this in my config.fish:
contains '.' $PATH; or set --export PATH $PATH .
You could do similar for adding ./bin, again in the last position:
contains './bin'; or set --export PATH $PATH ./bin
Two things are going on here:
This is setting $PATH directly, just as with other shells. This won't persist across shells.
This checks that it's not already in the $PATH, so that sub-shells won't get longer, redundant entries in $PATH

Resources