Score:0

Change default $PATH environment variables

br flag

I want to change default PATH Variable in bash. Also, I know can write export PATH=$PATH:/path/to/commands but it's not my solution. I want any apps uses system-call, new path can be used.

For example in python code:

  • from os import system
  • system('my-special-command')

my-special-command was stored at /path/to/my/commands/dir/my-special-command

Score:2
cn flag

THe PATH is defined at different levels of the system. Processes and subshells inherit the setting from the parent shell. So if you export a path in the terminal, the change will be valid in the current shell and any subshell of that terminal.

While you can add a custom path for every executable you want to place everywhere on your system, this approach will quickly become unwieldy. You would need to add a directory to the PATH for any other application you install. It would limit the portability of your program.

Before we indicate how the PATH can be changed at a higher level, I would suggest considering other, better approaches.

Place your executable in one of the default paths

Instead, stuck to the conventional approach for your custom applications, just like Ubuntu maintainers stick to some conversions when configuring applications that come with the system.

For your custom applications, that means: install them anywhere you want. Then install an executable in one of the directories mentioned in the PATH.

For your custom programs, /usr/local/bin is the appropriate conventional places to put your executable. If you need the executable to be available on a per user basis, place the executable in ~/.local/bin instead. Leave /usr/bin to the management of the system software management.

The executable can be a binary, a symbolic link to the executable installed elsewhere, or a wrapper script.

Coding the path in your executable

Instead of relying on a non standard systemwide PATH setting in your program, define custom PATHS in your executable itself where to find resources. To make this portable, work with a base directory, i.e., the directory where the program is installed, and locate your resources in a directory structure underneath. At runtime, the executable can determine the path where it resides. Put that path in a variable, e.g. RUN_PATH, then locate your resources using a relative path, e.g. images under $RUN_PATHS/icons, etc.

Change the systemwide PATH

If you insist doing it your way, feel free. It is your system. This is a way to change the PATH variable early in the boot process. As explained before, this approach is difficult and unelegant to port. If everybody decided to work your way, it would lead to a large PATH with custom entries on user's system. A long path slows down finding and executing any executable on the system.

vanadium avatar
cn flag
@bac0n of course you are right: thanks for pointing out.
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.