RoY RoY - 2 months ago 7
Linux Question

Bash Script To Add A Folder/Directory to the path in linux not working

I created a bash script to add

/My_Scripts/Bash_Scripts

to the default PATH of linux.

!/bin/bash
#This Script is used to add a folder/diectory to the PATH..


echo -e "\e[92m\e[1mCREATING PATH...........\n\n"
cd
mkdir My_Scripts
cd My_Scripts
mkdir Bash_Scripts
cd

export PATH=$PATH:$HOME/My_Scripts/Bash_Scripts
echo -e "\e[92m\e[1mPATH CREATON SUCCESSFUL\n \e[39m"
echo $PATH


The output of script is

root@kali:~/Desktop# bash add_path
CREATING PATH...........


PATH CREATON SUCCESSFUL

`/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/root/My_Scripts/Bash_Scripts'


but if I type
echo $PATH
in the terminal outside, the path is not added

root@kali:~/Desktop# $PATH
bash: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin: No such file or directory

Answer

First thing - you should use echo $PATH. By simply typing $PATH you're trying to execute the command, hence the "No such file or directory error"

Next - the /root/My_Scripts/Bash_Scripts wasn't really added to the PATH. The first output you see in done inside the script, so the changes could be seen there.

The reason is that PATH will be set only in the context of the script shell, execute it as source add_path to preserve the changes in variables (but only for current shell).

If you want the variable to be persistant in all shells - add it to /.bashrc (since you're runnung as root).

Comments