1

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

4
  • 1
    It seems the other question you linked to has been resolved. Also the script snippets in your question show a lot of typos, such as a spurious blank between #! and /bin/sh, or /user instead of /usr. Could you update your question and verify that your problem persists with the typos fixed?
    – Tilman
    Commented Jun 2, 2023 at 18:23
  • Welcome to the Ask Ubuntu community. What @Tilman said. I'll also add that questions that are not related to the Ubuntu OS specifically can usually be addressed more directly via the Unix & Linux SE community.
    – richbl
    Commented Jun 3, 2023 at 0:24
  • Now, scripts that are not 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 to source ~/.bashrc either as you still are in the same subshell.
    – Hannu
    Commented Jun 3, 2023 at 6:56
  • The right way to do this is to not do it. Output a message like "To add this program to your 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/…
    – muru
    Commented Jun 7, 2023 at 9:23

0

You must log in to answer this question.

Browse other questions tagged .