Is there some best practice of adding a path to $PATH via a shell script permanently via a shell script and have it accessible? I believe I have only seen temporary solutions, via export PATH=$PATH:<path/to/add>
but suggested solutions like
echo "export PATH=\"<path/to/add>:\$PATH\"" >> ~/.bashrc
seem to add a "export PATH..." line every time the script is called. I am looking for something like this:
<install.sh>
#! /bin/sh
new_path="/foo/bar"
echo $PATH
# set path variable
updatePath($PATH, new_path) <---- how to do this
#load path variable in current script
source ~/.bashrc
echo "updated path":
echo $PATH
expecting the following output of running ./install.sh
:
/usr/local/bin:/usr/bin
updated path:
/usr/local/bin:/usr/bin:/foo/bar
Any working solutions or best practices? I want to have the /foo/bar stored in $PATH permanently and accessible directly within the install.sh script as well as from the command line. Ubuntu 22.04.2 LTS
#!
and/bin/sh
, or/user
instead of/usr
. Could you update your question and verify that your problem persists with the typos fixed?source
'd do run in a subshell, so will not update the current (launcher) shell - might this be the actual "problem" here? - it does not help tosource ~/.bashrc
either as you still are in the same subshell.PATH
, add the following line to your.profile
,.bashrc
or other preferred shell configuration file:export PATH="<path/to/add>:$PATH"
" and let the user do it. It's not your place to go messing around my shell config files..bashrc
may not even be the right place if they're not using bash. See also: askubuntu.com/questions/1026332/does-pip-have-autocomplete/…