What is your path and making scripts executable
What is the PATH?
When you type a command into the terminal (for example bowtie2), the shell must locate the program file associated with that command.
The shell does this by searching through a special environment variable called PATH.
The PATH variable contains a list of directories that the shell searches, in order, whenever you try to run a command.
If the executable file exists in one of those directories, the program runs.
If it is not found in any of them, you will see an error.
Viewing your PATH
To see the contents of your PATH variable, type:
echo $PATH
Here:
PATHis the name of the variable$tells Bash you want to access its valueechoprints that value to the screen
A typical PATH looks something like this:
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/yourname/bin
Each directory is separated by a colon (:).
The shell searches these directories from left to right.
Why PATH matters
If a program is inside one of the directories listed in your PATH, you can run it from anywhere in you file hierarchy:
bowtie2
However, if the program is not in your PATH, you must either:
- Provide its full (absolute) path:
/Users/yourname/tools/bowtie2
- Or add its directory to your PATH.
Many systems include common directories (such as /usr/bin) in PATH by default. Some users also create a ~/bin directory for their own scripts and add it to their PATH.
Making files executable
When you create your own script (for example touch test.sh), it is not automatically executable.
Every file on a Unix-like system has three types of permissions:
- Read (
r) – ability to view the file - Write (
w) – ability to modify the file - Execute (
x) – ability to run the file as a program
These permissions are defined for three categories of users:
- Owner
- Group
- Others
Viewing permissions
To see file permissions, use:
ls -l
Example output:
-rw-r--r-- 1 user staff 120 Feb 10 12:00 test.sh
The first 10 characters, -rw-r--r-- describe permissions.
Breakdown:
- First character: file type (
-= regular file,d= directory) - Next three: owner permissions
- Next three: group permissions
- Final three: others permissions
In this example:
- Owner:
rw-(read and write, not executable) - Group:
r--(read only) - Others:
r--(read only)
Because there is no x in the owner position, this file is not executable.
Trying to run a non-executable file
If you try:
./test.sh
You will see Permission denied.
Making a file executable
To make the file executable:
chmod +x test.sh
Now ls -l will show:
-rwxr-xr-x
The x indicates execute permission has been added.
You can now run the script using:
./test.sh
Important:
chmod +x gives execute permission to all user categories (owner, group, and others). More precise permission control is possible (for example chmod u+x), but that is beyond the scope of this introduction.
Move on to Installing packages, or back to Scope.