User Tools

Site Tools


nix:pathvars

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
nix:pathvars [2020/01/06 14:11] kauffmannix:pathvars [2020/01/06 15:51] kauffman
Line 1: Line 1:
 ====== PATH Variables ====== ====== PATH Variables ======
-I'm not sure how one first gets exposed to path variables, only that it can be confusing at first glance. This document tries to remedy that without going into the history of everything.+I'm not sure how one first gets exposed to path variables, only that it can be confusing at first glance. This document tries to remedy that without going into the history of **everything**.
  
  
Line 22: Line 22:
   * ''%%/usr/lib%%''   * ''%%/usr/lib%%''
   * ''%%/usr/local/lib%%''   * ''%%/usr/local/lib%%''
-   
  
 +===== Path Search =====
 +Ever wonder what happens when you type in a command (''%%ssh%%'') into the terminal and somehow your shell knows to launch that program.
  
 +Assuming the command is not a shell command, the shell will check the appropriate variable which includes search locations to launch the program you wish to execute.
 +
 +In our example, ''%%ssh%%'' is located in ''%%/usr/bin%%'' on my machine.
 +
 +We can find out where it by using the command ''%%which%%'' which (hehe!) will traverse our search paths to find the executable we are looking for.
 +<code>
 +user@linux1:~$ which ssh
 +/usr/bin/ssh
 +</code>
 +
 +===== Intro the the PATH variable =====
 +
 +Generally your search path for binaries will look something like the below.
 +<code>
 +/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
 +</code>
 +
 +We can see what is in our path variable by using the command ''%%echo%%'' to check the contents of the variable that contains our search paths. This variable is called ''%%PATH%%''.
 +
 +<code>
 +user@linux1:~$ echo $PATH
 +/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
 +</code>
 +
 +When a search occurs it will take the left most path, search that directory, then proceed to the next path to its right IF and ONLY IF it does not find what it is looking for in the directory it is currently searching.
 +
 +Fore example: Lets take a look at the executable ''%%pip%%''.
 +
 +''%%/usr/local/bin%%'' is first in the search path so we'll see the version of ''%%pip%%'' installed by CS Techstaff.
 +<code>
 +user@linux1:~$ which pip
 +/usr/local/bin/pip
 +</code>
 +
 +There also exists and pip executable in ''%%/usr/bin/pip%%'' but as discussed above the shell will use the binary first in the search path.
 +
 +
 +===== Setting up an Example =====
 +
 +Your shell will allow you manipulate the order in which these paths appear. It's as simple as setting a variable.
 +
 +For the following examples I assume you are using the bash shell, which is the default in CS. The concept will translate to other shells, though the syntax may be different.
 +
 +A common example to manipulate your path is if you have written a script and you'd like to invoke it without providing the full path:
 +
 +Lets create the directory ''%%bin%%'' in the root of our home directory:
 +<code>
 +user@linux1:~$ mkdir ~/bin
 +</code>
 +
 +We will place our example script that echo's 'Hello World!' into ''%%~/bin/hello%%''
 +<code>
 +#!/bin/bash
 +echo 'Hello World!'
 +</code>
 +
 +and make it executable.
 +<code>
 +user@linux1:~$ chmod +x ~/bin/hello
 +</code>
 +
 +Now that we have our executable ''%%hello%%'' we'd like to be able to call it without prepending the path like this: ''%%~/bin/hello%%''
 +
 +As you may have guessed we will want to add (pre or postpend) ''%%~/bin%%'' to our ''%%PATH%%'' variable.
 +
 +===== Manipulating the PATH variable =====
 +
 +<code>
 +$ export PATH=$PATH:~/bin
 +</code>
 +
 +To test we will need the shell to reevaluate $PATH.
 +<code>
 +$ hash -r
 +</code>
 +
 +Now we can see that ''%%hello%%'' is in our path and we can execute it without the full path.
 +<code>
 +user@linux1:~$ which hello 
 +/home/user/bin/hello
 +
 +user@linux1:~$ hello
 +Hello World!
 +</code>
 +
 +==== Making the change permanent ====
 +Add the following line to a file called ''%%~/.bash_profile%%''.
 +
 +   export PATH=$PATH:$HOME/bin
 +
 +This will postpend our custom path to the search path every time we launch a bash login shell.
 +
 +If you'd like to prepend just move your path in front of ''%%$PATH%%''. Just make sure each path is separated by a colon ''%%:%%''.
 +
 +    export PATH=$HOME/bin:$PATH
  
/var/lib/dokuwiki/data/pages/nix/pathvars.txt · Last modified: 2020/01/06 16:00 by kauffman

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki