Linux add app to path

Add / Remove a Folder to $PATH variable in Ubuntu 22.04

Want to configure your $PATH variable? Here’s the beginner’s guide shows how to do the trick in Ubuntu, though it works in most Linux systems.

Why adding custom folders to $PATH

PATH is an environment variable. It tells which folders to look for when trying to run executable file from command line.

Say you downloaded an executable script from the web, and saved it in ‘Downloads‘ folder. Before adding to $PATH, you need to either navigate to the folder that contains the file, and run it in terminal via:

cd ~/Downloads && ./executable-file

Or type the full path to file every time to start the script:

After moving the script into one of $PATH folders, such as /usr/bin , /usr/local/bin , ~/.loca/bin/ , you do NOT have to type so much anymore, just input the filename in terminal, and hit Enter. Linux will find it from $PATH folders, and run it if exist, or output command not found if can’t find it.

However, some apps have executable file as well as required run-time libraries in same folder. Moving the whole folder into one of $PATH folders will NOT work, because Linux does not find executable from sub-folders in $PATH.

Or, your project is still in developing stage. You want to try it out easily without installing into a PATH folder. So, add the working directory as $PATH variable is helpful.

Which file to edit: /etc/environment, /etc/profile, .bash_profile, .bash_login, .profile, or .bashrc

There are quite a few places to specify $PATH variable. They include /etc/environment , /etc/profile , scripts under /etc/profile.d , /etc/bash.bashrc , .bash_profile , .bash_login , .profile , and .bashrc .

Here I’m going to show you which file you should use for configuring $PATH variable.

/etc/environment

Ubuntu and its based Linux systems has a line in “ /etc/environment ” file that specify system wide $PATH variable. It’s:

Meaning the directories separated via “:” are in path for all users for all the cases.

Читайте также:  Linux kernel process management

However, it’s NOT recommended for editing this file to set $PATH variable. Any incorrect setup MAY cause login issues!!

/etc/profile

To add a folder to $PATH variable for all users, “/etc/profile” is the right place for login shell (common login via user password, SSH login, or via su — username ).

Instead of manually editing the /etc/profile file, many applications automatically create scripts under “ /etc/profile.d ” to do the same job.

It however does not work for non-login shell. For example, you log into Linux via user-A, then open terminal and use su user-B to switch to user B. In the case, $PATH settings in ‘/etc/profile’ works for user-A, but NOT for user-B in this terminal. As a workaround, use su – user-B instead.

.bash_profile, .bash_login, .profile

For current user only, there are 3 files to specify $PATH variable for login shell. They are hidden files in user home directory: ‘.bash_profile‘, ‘.bash_login‘, ‘.profile‘.

On user login, it first reads “ /etc/profile ” (& scripts under /etc/profile.d ). Then, tries to:

  • run ‘ .bash_profile ‘ if exist and skip 2 others.
  • if ‘ .bash_profile ‘ not exist, use .bash_login if available and skip .profile .
  • take use .profile only when the previous 2 are not exist.
For Ubuntu & its based systems, only .profile exist out-of-box.

/etc/bash.bashrc & .bashrc

The 2 files are used for configuring alias and other bash functions for non-login shell, such as terminal access via su username . While /etc/bash.bashrc is for system wide, .bashrc works for current user only.

However, /etc/profile in most Linux includes rules to run /etc/bash.bashrc automatically. And, .bash_profile, .bash_login, or .profile also tells to run .bashrc automatically. As a result, the 2 files work in both login and non-login shell.

Add/Remove a folder to $PATH variable

After reading the content above, you should know which file to edit for setting your $PATH.

In short, for login shell:

  • For system wide, edit /etc/profile file or create script under /etc/profile.d .
  • For current user only, edit ~/.bash_profile (if exist), or ~/.bash_login (if former one not exist), or ~/.profile .

For non-login shell (and login shell, because previous files will also run the 2 files below):

Edit file and add a folder to $PATH

Now, open a terminal window (press Ctrl+Alt+T on Ubuntu) and run command to edit a config file.

Replace ~/.bashrc with one of previous files you want to edit. Also, replace nano with your favorite text editor. And, add sudo for editing files under /etc/

When file opens, scroll to end and add new line or lines:

  • For example, add the “work” folder in user home to $PATH variable, no matter if the folder exists or not:
    export PATH=$HOME/work:$PATH
  • Add “work” folder in user home to $PATH only when it exists.
    if [ -d «$HOME/work» ] ; then
    PATH=»$HOME/work:$PATH»
    fi
Читайте также:  All information about linux operating system

Verify your $PATH variable

After log out and back in, you may run the command below in terminal to output current $PATH variable:

There may be “No such file or directory” that occurs when a folder added to $PATH, but later removed. It does not matter, but can be avoided by using “if” statement when adding to $PATH.

See How to Add folder to PATH via YouTube Video:

Summary

In general, user can edit ‘/etc/profile‘ or add scripts under ‘/etc/profile.d‘ to configure $PATH for system wide.

For current user only, edit ‘~/.bash_profile‘ if exist, or ‘~/.bash_login‘. If non of the 2 exists, edit ‘~/.profile‘.

In case you want to make it work for non-login shell, such as switch to user in terminal via su username , edit ‘/etc/bash.bashrc‘ or ‘~/.bashrc‘ instead.

Источник

Linux: Add a Directory to PATH

PATH is an environment variable that instructs a Linux system in which directories to search for executables. The PATH variable enables the user to run a command without specifying a path.

This article will explain how to add a directory to PATH temporarily or permanently as well as how to remove it in Linux.

Linux: add a directory to PATH

What Is Linux PATH?

When a user invokes a command in the terminal, the system executes a program. Therefore, Linux has to be able to locate the correct executable. PATH specifies program directories and instructs the system where to search for a program to run.

How to View the Directories in PATH

To print all the configured directories in the system’s PATH variable, run the echo command:

echo PATH terminal output

The output shows directories configured in PATH by default. The printenv command delivers the same output:

printenv PATH terminal output

Furthermore, running which on a certain command shows where its executable is. For instance, execute which with whoami :

which whoami terminal output

The output shows that the executable for whoami is located in the /usr/bin/ directory.

How Do I Add a Directory to PATH in Linux?

Specific directories are added to PATH by default. Users can add other directories to PATH either temporarily or permanently.

Linux: Add to PATH Temporarily

Temporarily adding a directory to PATH affects the current terminal session only. Once users close the terminal, the directory is removed.

To temporarily add a directory to PATH , use the export PATH command:

export PATH="/Directory1:$PATH"

export PATH terminal output

The command added Directory1 from the Home directory to PATH . Verify the result with:

echo PATH Directory1 added terminal output

The output shows that the directory was added to the variable. This configuration lasts during the current session only.

Читайте также:  Linux using built in drivers

Linux: Add to PATH Permanently

Add a directory to PATH permanently by editing the .bashrc file located in the Home directory. Follow these steps:

1. Open the .bashrc file using a text editor. The example below uses Vim.

Opening .bashrc in Vim.

3. Paste the export syntax at the end of the file.

export PATH="/Directory1:$PATH"

Add directory to .bashrc file in Vim

5. Execute the script or reboot the system to make the changes live.

6. To verify the changes, run echo :

echo PATH permanently added Directory1 terminal output

Editing the .bashrc file adds a directory for the current user only. To add the directory to the PATH for all users, edit the .profile file:

Add directory to profile file in Vim

Remove Directory from PATH in Linux

There is no single command to remove a directory from PATH . Still, several options enable the process.

Method 1: Exit the Terminal

Removing a directory from PATH is simple when it’s added temporarily. Adding the directory in the terminal works for the current session only. Once the current session ends, the directory is removed from PATH automatically.

To delete a temporary directory from PATH , exit the terminal or reboot the system.

Method 2: Edit Configuration Files

If the directory export string was added to the .bashrc or .profile file, remove it using the same method. Open the file in a text editor, navigate to the end of the file, and remove the directory.

Method 3: Apply the String Replacement Concept

To remove a directory from PATH , use string replacement:

String replacement terminal output

The command only removes the string from the current session.

Method 4: Use a One-Liner

Another option is to use the combination of tr, grep and paste to remove a directory from PATH . For instance:

export PATH="$( echo $PATH| tr : '\n' |grep -v Directory1 | paste -s -d: )"

tr grep -v paste terminal output

After reading this guide, you now know how to add a directory to the PATH variable. Next, learn how to export Bash variables in Linux.

Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.

The printf command produces formatted text outputs in the terminal. This tutorial shows how to use the printf command to print and format a variable output in Linux.

The cd command in Linux allows you to change the current working directory using the terminal window. Learn how to use the cd command in this tutorial.

Linux offers different ways to remove directories without using the GUI. In this tutorial, learn how to remove directories in the terminal window or command line using Linux commands.

Источник

Оцените статью
Adblock
detector