Following this guide,
I don't get the last line :
Don't forget to add the PATH to your .bashrc to make it permanent.
What exactly should I be doing ?
The statement you quoted is about the export PATH=$PATH:/usr/local/bin line from
For testing add /usr/local/bin to your path
export PATH=$PATH:/usr/local/bin
node -v
npm -v
The fragment PATH=$PATH:/usr/local/bin adds the path /usr/local/bin to the content of the PATH environment variable. This variable is used by the shell (bash) to search for executable programs.
The export in front of it tells bash to forward the updated PATH variable to all programs it launches.
Adding the line to .bashrc makes it run again every time you start a new bash instance (the environment variables are not stored anywhere, their values are set when bash starts).
/usr/local/bin is a standard path. Most probably you don't have to add that line to .bashrc or execute it at all. Use echo $PATH to check. If its output already contains /usr/local/bin (or contains it twice if you already executed the line above) then you don't need to do anything else. bash is able to find node and npm.
If /usr/local/bin is not in the output of echo $PATH (or it is only once after you have executed export PATH=$PATH:/usr/local/bin then run:
echo 'export PATH=$PATH:/usr/local/bin' >> ~/.bashrc
to put export PATH=$PATH:/usr/local/bin on a new line at the end of ~/.bashrc.
Related
This question already has an answer here:
Unable to export the variable through script file [duplicate]
(1 answer)
Closed 6 years ago.
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
You are starting up a new bash process and PATH is only modified in the context of the new process. When this process quits, changes done in its environment do not propagate to the parent process.
Instead, you would want to modify PATH in the context of current bash process. If you want this temporarily, you may source your script. source will run in the context of the current bash process. Beware of any side effects - like cd will change the directory, exit will terminate the current bash process.
If you want this change permanently for all future interactive sessions, you can modify ~/.bashrc.
Also, the syntax of shebang is #!/path/to/program, you are missing a #.
Your changes is affected in current shell only.Put the entry in .bashrc file. It will affect the all the terminal.open the .bashrc file and add the below line and run the file -
vim ~/.bashrc
export PATH="$PATH:/home/username"
~/.bashrc
Edit the parent shell
script.sh
#!/bin/bash
export "PATH=$PATH:$HOME/My_Scripts/Bash_Scripts"
echo $PATH
$~ PATH=$(./script.sh)
$~ echo $PATH
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/loganaayahee/My_Scripts/Bash_Scripts
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).
I'm unable to use ls, bash.. any popular commands that are critical after changing the path.
I'm unsure what it was before (because I can't do vi command either).
I ran the first command, and realized the first one had a typo - not PATH, but I've typed PATh.
So I immediately ran the next one:
export PATH="/usr/local/bin:$PATh"
export PATH="/usr/local/bin:$PATH"
Then vi, ls, bash commands started to not work.
I did echo $PATH to see the PATH.
usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/bin:
This is what I got. Any help is appreciated.
You should be able to source /etc/profile to reset your PATH variable, though it may step on a few other variables you've configured along the way. You could also just grep for the appropriate line from that to set PATH and redo that in your current environment
Also, you can always specify the full path to an executable you need in the interim. For example, if you wanted to use grep with the PATH in its current state you could use /bin/grep (or perhaps /usr/bin/grep depending your system)
1 > Try to load default .profile script
$ /bin/bash ./.profile
2 > Just Logout from current session
and Re-login it.
It appears you have "broken" your bash shell ~/.bash_profile script, because of the PATh typo. (The question explicitly states bash, so I'm answering in that context.)
Since the PATH is "broken", you will need to access your editor by using its fully qualified path.
/usr/bin/vi ~/.bash_profile
That should allow you to fix the PATh to be PATH.
If you find that you need to edit your PATH environment variable a lot, this little bash function can be helpful.
vipath() {
local tmpfile=$(mktemp /tmp/vipath-edit.XXXXXX)
echo "$PATH" | tr ':' '\n' > "$tmpfile"
vi "$tmpfile" && PATH=$(paste -s -d ':' "$tmpfile")
rm "$tmpfile"
}
Note: there are better ways to ensure the $tmpfile gets deleted that I did not use in this snippet. And on a multiuser system, there are better ways to make sure the temporary file is not located in a shared location.
If you want to add a directory location to your PATH, without adding duplicate locations, this little bash function can be helpful to prepend a directory location.
pathadd() {
if [ -d "$1" ] && [[ ! ":$PATH:" =~ ":$1:" ]]
then
PATH="$1:$PATH"
fi
}
I had the same in RHEL8, I did an export PATH for a certain directory and then no command worked anymore. I performed a faulty export PATH command probably.
I got messages like this:
>$ yum
bash: yum: command not found...
Install package 'yum' to provide command 'yum'? [N/y] n
Luckily I had some other similar systems where I could get the path from, so I did:
export PATH=/home/USER/.local/bin:/home/USER/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbine
Change USER to your own.
To make it permanent: add to $HOME/.bashrc with:
export PATH=$PATH:<YOUR PATH HERE>
When you do export PATH="/usr/local/bin:$PATH" you are saying, set PATH to /usr/local/bin: plus whatever was in the PATH variable before. This essentially concatenates the string "/usr/local/bin:" with the old path. That is why your PATH repeats so much, you must have run that command a few times.
Try running this: export PATH="/usr/local/bin".
I have a few questions on this $PATH in Linux.
I know it tells the shell which directories to search for executable files, so:
What does it mean an environmental variable?
How to change its path? and is it recommended to change it?
IF i change it what are the consequences?
To get your path current $PATH variable type in:
echo $PATH
It tells your shell where to look for binaries.
Yes, you can change it - for example add to the $PATH folder with your custom scripts.
So: if your scripts are in /usr/local/myscripts to execute them you will have to type in a full path to the script: /usr/local/myscripts/myscript.sh
After changing your $PATH variable you can just type in myscript.sh to execute script.
Here is an example of $PATH from RHEL:
/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/user/bin
To change your $PATH you have to either edit ~/.profile (or ~/.bash_profile) for user or global $PATH setting in /etc/profile.
One of the consequences of having inaccurate $PATH variables is that shell will not be able to find and execute programs without a full $PATH.
Firstly, you are correct in your statement of what $PATH does. If you were to break it somehow (as per your third point), you will have to manually type in /usr/bin/xyz if you want to run a program in /usr/bin from the terminal. Depending on how individual programs work, this might break some programs that invoke other ones, as they will expect to just be able to run ls or something.
So if you were to play around with $PATH, I would suggest saving it somewhere first. Use the command line instruction
echo $PATH > someRandomFile.txt
to save it in someRandomFile.txt
You can change $PATH using the export command. So
export PATH=someNewPath
HOWEVER, this will completely replace $PATH with someNewPath. Since items in path are separated by a ":", you can add items to it (best not to remove, see above) by executing
export PATH=$PATH:newPath
The fact that it is an environmental variable means that programs can find out its value, ie it is something that is set about the environment that the program is running in. Other environmental variables include things like the current directory and the address of the current proxy.
this is simple and i do like this way.
Open the linux bash shell and print the environment variables:
printenv
I copy "PATH" variable to a text editor and edit as I want. Then update the PATH like this
export PATH= /variable dir list/
It Works.
or if you want to add an single variable use this command.
export PATH = $PATH:/variable_dir_path/
This will extends the PATH with your new directory path.
I logged in as root to my CentOS 5/cPanel server and I typed the following:
cd /usr/local/bin/
git clone --depth 1 http://github.com/joyent/node.git
cd node
git checkout origin/v0.4 # optional. Note that master is unstable.
export JOBS=2 # optional, sets number of parallel commands.
mkdir ~/local
./configure --prefix=$HOME/local/node
make
make install
echo 'export PATH=$HOME/local/node/bin:$PATH' >> ~/.profile
source ~/.profile
It seems to be working fine until I log out from the server and log back in it's as it wasn't installed:
[~]# node test.js
-bash: node: command not found
If I type: source ~/.profile it starts working again until I log out.
Please help. Thanks.
EDIT:
This is the content of my .bash_profile, how should I change it?
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
unset USERNAME
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
Maybe you have a .bash_profile and it's being used instead?
In linux I would go:
setenv -p MYVAR "somevalue"
But this doesn't seem to work in cygwin.
By default Cygwin is running the Bourne shell or Bash, so the command to set a variable is different. This is the code you need:
export MYVAR="somevalue"
The export part lets the shell know that it is an environment variable instead of a local variable.
If you type ls -a in your home directory, you should see some or all of the following files:
.bashrc
.bash_profile
.profile
.bash_profile is executed for login shells, and .bashrc is executed for interactive non-login shells. To most simply ensure that your environment variable is always set, open up .bash_profile and add the text:
export MYVAR="somevalue"
Your shell with then execute .bash_profile every time it starts up, and it will run this command. You will then have the MYVAR variable accessible all of the time. If you didn't export the variable, it would only be accessible within your .bash_profile file.
You can check that this variable is defined by printing its value to your shell:
echo $MYVAR
You can delete (unset) the variable with:
unset $MYVAR
Brief words on shell config files
As an aside, regarding .bashrc vs .bash_profile vs. .profile, see these answers:
difference between .bash_profile and .bashrc
difference between .profile and .bash_profile
For simplicity of configuration, I recommend sourcing your .bashrc file from .bash_profile. Add this to .bash_profile:
if [ -f ${HOME}/.bashrc ]; then
source ${HOME}/.bashrc
fi
This will load .bashrc from .bash_profile.
If you do this, you can instead put the following line in .bashrc, if you wish:
export MYVAR="somevalue"
The best way to set up environment variables in cygwin is to create a bash profile and execute that profile everytime you login and run the shell.
In my .bash_profile file , this is the setting I have
JAVA_HOME = C:/Program Files/Java/jdk1.7.0_51
export JAVA_HOME
export PATH=$PATH:$JAVA_HOME/bin
Once you run bash, check out echo $JAVA_HOME and you should see the path as output.