Languages
[Edit]
EN

Linux / Bash - add additional bin directory to system PATHs

7 points
Created by:
Root-ssh
116070

In this short article we would like to show how to add some program as command to environment PATH variable in Bash under Linux. Presented approach will help us to use as commands executable files located inside indicated path.

Quick solution (run command):

export PATH=/path/to/directory/with/bin:$PATH

Where: /path/to/directory/with/bin should be replaced with path to directory that has executable files.

Note: above command executed in Bash adds path only for current session - it means after command line is closed the path is removed.

Detailed instruction

In this section we would like to show practical example how to add my-script.sh as Bash command.

Simple steps:

  1. open Bash,
  2. create some script that has path ~/bin/my-script.sh and put inside some operations e.g.
    #!/bin/bash
    
    echo "Test!!!"
  3. add executable permissions for my-script.sh, (e.g. chmod a+x ~/bin/my-script.sh)
  4. run following command:
    export PATH=~/bin:$PATH
  5. go to any place e.g. /home
  6. run following command:
    my-script.sh

    Output:

    Test!!!

    Note: we can see, we don't need to use full path to run ~/bin/my-script.sh - Bash finds executable file automatically.

Console preview:

john@ubuntu:/mnt/john/$ cd ~/bin^C           
john@ubuntu:/mnt/john/$ nano ~/bin/my-script.sh
john@ubuntu:/mnt/john/$ chmod a+x ~/bin/my-script.sh                
john@ubuntu:/mnt/john/$ export PATH=~/bin:$PATH
john@ubuntu:/mnt/john/$ cd /home
john@ubuntu:/mnt/john/$ my-script.sh
Test!!!
john@ubuntu:/mnt/john/$

 

Linux

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join