Score:0

export path to $PATH via shell script

cz flag

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

Tilman avatar
cn flag
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?
ec flag
**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](https://unix.stackexchange.com/).
Hannu avatar
ca flag
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.
muru avatar
us flag
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: https://askubuntu.com/questions/1026332/does-pip-have-autocomplete/1026594#comment1669103_1026594
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.