EN
Linux / Bash - add additional bin directory to system PATHs
7
points
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:
- open Bash,
- create some script that has path
~/bin/my-script.sh
and put inside some operations e.g.#!/bin/bash echo "Test!!!"
- add executable permissions for
my-script.sh
, (e.g.chmod a+x ~/bin/my-script.sh
) - run following command:
export PATH=~/bin:$PATH
- go to any place e.g.
/home
- 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/$