0

I understand the code is trying to make the executable in $HOME/.rbenv/bin available in $PATH so it can be executed from the command line but I can't figure out how the code does this. The code is shown below:

echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> $HOME/.bashrc.

I know that something is being appended to the end of the bashrc file and I know export is used to export environment variables to new shells but I don't see any new shell here. And I have seen echo used in the following way: a = 5; echo $a but I can't figure out why you need it here and what is

PATH="$HOME/.rbenv/bin:$PATH"

doing. what does 'PATH' represent. Is it a variable without the $ and what is 'bin:$PATH'.

fineboy1
  • 21
  • 3

1 Answers1

0

In bash (and most other shells) you assign variables without the $, so PATH=something assigns the variable PATH to the string "something".

In Unix/ Linux, PATH is a string variable that contains a list of folders separated by colons (:). For example on my laptop this is my PATH:

> echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin

This means that any executable in those folders is accessible anywhere without having to type the full path. You will typically add things to the PATH in your .bashrc file, so they get added when your shell starts up. If you use the which command you can see where a command lives, it will be in one of these folders:

> which rm
/bin/rm

To add a new folder to the PATH, you re-assign PATH to be a string of the new folder, followed by a colon and the previous value of PATH.

In you example, you are adding $HOME/.rbenv/bin to the start of PATH. $HOME will be expanded to your home directory.

To understand this better we can do something like this:

 > echo $PATH               
/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
 > export PATH="$HOME/.rbenv/bin:$PATH"
 > echo $PATH                          
/Users/javanut13/.rbenv/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin

As you can see the rbenv folder was added to the front of the PATH variable.

export puts the variable into the environment of the current shell, which means that other commands started in that shell (subprocesses) also get this change. For example if you don't do export and then run a bash script that uses ruby, it will fail because it doesn't have the ~/.rbenv/bin folder in its path.

Will Richardson
  • 7,780
  • 7
  • 42
  • 56
  • Added that into the answer. – Will Richardson Apr 19 '18 at 18:08
  • Can you point me to some resources to learn about linux I seem to have some core concepts confused in my head. – fineboy1 Apr 19 '18 at 18:25
  • I don’t have any great suggestions, sorry. – Will Richardson Apr 19 '18 at 18:35
  • Let's say you open up a new terminal does the system look first in bashrc or $PATH to find executables. They seem very similar to me. – fineboy1 Apr 19 '18 at 18:37
  • When you open a terminal, it will start Bash, which will execute your `.bashrc` file, which sets the `PATH` (as you asked in your question). Then when you're using Bash, each time you run a command it will search through the `PATH` variable for executable files, and run the first match (or print an error if it cannot be found). So the `PATH` is entirely to do with Bash (or whichever shell) and doesn't matter to the system - when the "system" runs a command, it will use Bash (or a lighter-weight alternative) to first create a `PATH` and then run the command. – Will Richardson Apr 19 '18 at 19:41