1

I want to add PATH on Linux Ubuntu 18.04 from file path.sh , my file include :

 #!/bin/bash
 export PATH="$PATH:/root/.local/bin"

chmod+x path.sh , but when i run it ./path.sh Path not add when i type echo $PATH .

but when i type in terminal export PATH="$PATH:/root/.local/bin" it added to my PATH .

Am I miss something on my file?

2
  • Can't add an answer to this anymore, but a less common solution to this problem—particularly if source doesn't fit your needs—is to leverage exported functions and/or aliases to update your PATH. function myPath() { export PATH="/whatever/bin:$PATH" } export -f myPath Then, add this to your .bashrc or equivalent: source ~/myPath.sh (apologies in advance for crappy comment formatting restrictions) Commented May 3, 2024 at 23:20
  • ^ Really handy alternative if, for example, you have a bin directory on an external drive or perhaps a partition that isn't always mounted, mounted at different paths on different machines, etc. Personally I put my mount/umount shortcuts into exported functions like these to handle both mounting and updating the environment (PATH etc) in one swoop. Commented May 3, 2024 at 23:27

2 Answers 2

2
source path.sh

add this step is fine.

Sign up to request clarification or add additional context in comments.

Comments

1

Let's consult man bash:

export [-fn] [name[=word]] ...
export -p
       The  supplied names are marked for automatic export to the envi-
       ronment of subsequently executed commands.

Note "subsequently executed commands", therefore the effect of your script ends, once the script ends.

1 Comment

I don't think this really answers OP, or at least not very clearly. In the absence of your additional commentary, the quoted man entry seems to already describe OP's working understanding of export—it adds noise, without offering new insight. More direct insight sans manpage: (1) Running a script (./) creates a child shell of the calling shell, and inherits a copy of its environment; and (2) export can only propagate forwards in this inheritance tree, and cannot copy variables "backwards" to the calling environment after exiting. This is why running doesn't work, but source does.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.