092013

Modifying the system PATH variable Andrew Mallett | September 2013

Dennis Ritchie 1941-2011Dennis Ritchie (the co-founder of Unix) famously noted that, "Unix is simple. It just takes a genius to understand its simplicity." And I couldn't agree more. Like many, the more complex tasks I undertake, the more I look for simple solutions. This is so true in the case of shell-scripts where a few lines of code (in plain text!) can perform complex functions.

So because I believe in not making things any more complicated than they need to be, I like to keep all my custom scripts in one directory..

/sc

This provides a number of advantages, including quick access and ease of backup. It also means I can run these scripts from anywhere by having the /sc directory in the system path.

Paths and environments

When you run a command, the system will look in the $PATH statement to find the location of that command. If it can't find the executable (and you haven't typed in its full path), then you will get a message indicating the command has not been found. Unix doesn't even look in the current directory, which can seem a bit odd if you've come from other DOS-like command-line operating systems. DOS will look in the current directory before checking on the path variable.

Tell Unix to look in the current directory for the required command with a dot and a forward slash..

./command

By putting my scripts directory in the system path, I avoid some of this quirkiness and can run them from anywhere in the file system. The following command will reveal the hidden secrets of your system path..

echo $PATH

There are a few ways to append a new directory to the system path. Under Ubuntu and similar distros I simply edit the /etc/environment file..

PATH="/sc:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"

Other options include editing the PATH statement in the relevant file in your home directory, which might include ~/.profile or ~/.bashrc or ~/.bash_profile, which you may need to create and assumes you'll be using the BASH shell

The change will stick the next time you log into X-Windows or the terminal session.

A word on executables

Users coming from a DOS/Windows environment will be used to file extensions which indicate a file is executable, such as .com, .bat and .exe. Unix does not require a file extension, although some files may have them. Any file can be made executable using the chmod command..

chmod 755 filename

I like to name my shell scripts using the .sh extension so that they are immediately apparent. Also I can 'associate' this file extension with a text reader for quick and convenient reading. Why make things harder..?

 

Back
More custom scripts at Shell-scripts.com

Comments (1)

Paths
This is also important because unlike DOS, Unix doesn't look in the current directory for a file.
#1 - RHamill - 02/01/2015 - 08:08
Name
E-mail (Will not appear online)
Homepage
Title
Comment
;-) :-) :-D :-( :-o :-O B-) :oops: :-[] :-P
To prevent automated Bots form spamming, please enter the text you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
»
This comment form is powered by GentleSource Comment Script. It can be included in PHP or HTML files and allows visitors to leave comments on the website.

Disposable Email   Temporary Email   Throwaway Email