Creating a PATH variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating a PATH variable
# 1  
Old 12-26-2018
Creating a PATH variable

I am new to shell scripting and I ran into a couple lines of code which I don't completely understand:

Code:
PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/entity/bin
data_dir=/usr/local/entity/project

I believe data_dir to be a more conventional link to a directory. However, I am not sure what PATH is. It also seems like a location but maybe it's multiple possible locations? Does the : signify that the location could be either: /usr/bin, /bin, /usr/sbin or /usr/local/entity/bin? Is the : in this context similar to some function like [ is to the test function?
# 2  
Old 12-26-2018
Hi, that is where the shell looks for executables.
Quote:
PATH
This variable shall represent the sequence of path prefixes that certain functions and utilities apply in searching for an executable file known only by a filename. The prefixes shall be separated by a <colon> ( ':' ). When a non-zero-length prefix is applied to this filename, a <slash> shall be inserted between the prefix and the filename if the prefix did not end in <slash>. A zero-length prefix is a legacy feature that indicates the current working directory. It appears as two adjacent <colon> characters ( "::" ), as an initial <colon> preceding the rest of the list, or as a trailing <colon> following the rest of the list. A strictly conforming application shall use an actual pathname (such as .) to represent the current working directory in PATH. The list shall be searched from beginning to end, applying the filename to each prefix, until an executable file with the specified name and appropriate execution permissions is found. If the pathname being sought contains a <slash>, the search through the path prefixes shall not be performed. If the pathname begins with a <slash>, the specified path is resolved (see Pathname Resolution). If PATH is unset or is set to null, the path search is implementation-defined.
Since <colon> is a separator in this context, directory names that might be used in PATH should not include a <colon> character.
The Open Group Base Specifications Issue 7, 2018 edition
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 12-27-2018
Or, think of the PATH as your favorites. An ordered list of places to look for programs.
In your example, there were five BIN directories to search for any referenced programs.
And the BIN directories are where executable programs are typically stored.
This User Gave Thanks to joeyg For This Post:
# 4  
Old 12-27-2018
Hi,

It is quite common to change the order of the PATH directories, as an example on Solaris you may want to pick up a different version of a binary - for instance the posix compliant version of awk.

Regards

Gull04
This User Gave Thanks to gull04 For This Post:
# 5  
Old 12-27-2018
Quote:
Originally Posted by Circuits
I believe data_dir to be a more conventional link to a directory.
No. In fact it is a "variable". Suppose the following: i don't know where you installed a certain program, but i know how the place where the program is installed looks like because i work with it a lot. So, if you ask me where you can find the logs of the program, i'd tell you: "go to where you installed the program". In there, whereever that is, you find a directory "<somewhere>/logs" and inside of this you find a directory "<somewhere>/log/program-logs", ... Now, you might ask me something else about this program and again, i'd refer to the (to me unknown) location of the program as "<somewhere>" and direct you to some plcae relative to there.

This is what variables are for: you define them by assigning some value (in your case the value /usr/local/entity/project) to a name (data_dir) and then you can use it everywhere without needing to know the exact value of it. Just like i used the name "<somewhere>" above. I could have used a variable:

Code:
program_location= .....    <= you would have to fill in the actual path here, say: /usr/myprogram

and in this case the logs would have been there:

Code:
program_location=/usr/myprogram
cd $program_location/logs
cd $program_location/logs/program-logs

The second and third lines use the variable we have defined before: $program_location is first replaced by what we assigned to the name "program_location" before, only then "/logs" or respectively "/logs/program-logs" is appended. So the real commands read:

Code:
cd /usr/myprogram/logs
cd /usr/myprogram/program-logs

Quote:
Originally Posted by Circuits
However, I am not sure what PATH is. It also seems like a location but maybe it's multiple possible locations? Does the : signify that the location could be either: /usr/bin, /bin, /usr/sbin or /usr/local/entity/bin? Is the : in this context similar to some function like [ is to the test function?
PATH is a special kind of variable. Or, rather, a normal variables (everything i said above applies) but with a special function. You see, a program is a certain file (where executable code is stored). To execute the program you have to write the full name of the program file:

Code:
/path/to/the/executable/file/of/my/program

As the pathes get longer this might be a lot of work to type. Computer people are in general of the lazy sort (this is why we use computers to do it for us). Since many programs are collected in a few directories (i.e. most of the systems commands are stored in /usr/bin) the PATH variable was invented. It holds a list of directories, separated by ":". The rule is: if a program is located in one of the directories in this list then you do not need to specify the path when calling it.

That means: your path looks like this:

Code:
PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/entity/bin

This is a list of directories: /usr/bin, /bin, /usr/sbin, /sbin and /usr/local/entity/bin. For instance there is a command cp (it copies files) located in /usr/bin. So, to call it and copy the file "file" to a new file called "new_file" you would have to use the command

Code:
/usr/bin/cp file new_file

But since /usr/bin is included in your PATH variable you can omit that and write:

Code:
cp file new_file


I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 6  
Old 12-27-2018
Just want to make sure I understand completely. Specifying the PATH variable means that the program will automatically assume a path for functions like cp or what-have-you without actually referencing the variable: PATH? This would make sense because the deceleration of PATH is the only place in the script where it is being referenced by name. Then again, the only place data_dir is being referenced by name is in the instantiation as well. Is it possible that data_dir is acting in a similar way to PATH or is it more likely that data_dir was just never used after it was declared?
# 7  
Old 12-27-2018
PATH is something that tells the computer where to look for programs, and in what order to search for the programs.
By programs, I am referring to many of the commands you use in unix - cp, rm, mv, cat - are actually programs.
So, if you type 'cat myfile', unix would look in /usr/bin folder first to find the program cat.exe; then it will do what cat is programmed for, display the contents of the parameter you typed, myfile

Defining data_dir is a common programming step. This avoids the need to specify it everywhere in your script, and (MOST IMPORTANTLY) allows you to adjust the location just once rather than throughout your script.

thus, in your script you could have the following commands (if the archiv_dir was also defined):
cat $data_dir"/workfile.txt"
mv $data_dir"/workfile.txt" $archv_dir"/workfile.arc"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Readin document & creating path

Need a way to read a file in who every line is a path to a directory and make shortcut to that directory on a specific place. Example: line in the document /media/gogo/6651-FEAB/Desktop/ /media/gogo/6651-FEAB/Desktop/alex/ /media/gogo/6651-FEAB/linux/ ... (3 Replies)
Discussion started by: gogok_bg
3 Replies

2. Shell Programming and Scripting

File creating in another path.. application unable to locate

I am submitting a concurrent program (of HOST tyme) from Oracle apps screen, The MAIN shell program submits another program, (child) which is also a Shell program. The child writes data to log file. Now the main program, read the log and do some calculations and sends the data to user through... (1 Reply)
Discussion started by: Pradeep Garine
1 Replies

3. Shell Programming and Scripting

Path a variable to sed that includes a path

Hi I'm trying to select text between two lines, I'm using sed to to this, but I need to pass variables to it. For example start="BEGIN /home/mavkoup/data" end="END" sed -n -e '/${start}/,/${end}/g' doesn't work. I've tried double quotes as well. I think there's a problem with the / in the... (4 Replies)
Discussion started by: mavkoup
4 Replies

4. Shell Programming and Scripting

Appending a path in user's PATH variable

Hello Folks, I want to append a path in user's PATH variable which should be available in current session. Background Numerous persons will run a utility. Aim is to add the absolute path of the utility the first time it runs so that next runs have the PATH in env & users can directly run... (6 Replies)
Discussion started by: vibhor_agarwali
6 Replies

5. Shell Programming and Scripting

one liner to extract path from PATH variable

Hi, Could anyone help me in writing a single line code by either using (sed, awk, perl or whatever) to extract a specific path from the PATH environment variable? for eg: suppose the PATH is being set as follows PATH=/usr/bin/:/usr/local/bin:/bin:/usr/sbin:/usr/bin/java:/usr/bin/perl3.4 ... (2 Replies)
Discussion started by: royalibrahim
2 Replies

6. Shell Programming and Scripting

remove a path from PATH environment variable

Hi I need a script which will remove a path from PATH environment variable. For example $echo PATH /usr/local/bin:/usr/bin:test/rmve:/usr/games $echo rmv test/rmve Here I need a shell script which will remove rmv path (test/rmve) from PATH... (9 Replies)
Discussion started by: madhu84
9 Replies

7. Shell Programming and Scripting

Sed variable substitution when variable constructed of a directory path

Hello, i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem. my var1 is a string constructed like this: filename1 filerev1 filepath1 my var2 is another string constructed like this: filename2 filerev2 filepath2 when i do... (2 Replies)
Discussion started by: alrinno
2 Replies

8. Shell Programming and Scripting

Getting the path from a variable

Hi, I am having a variable Like line="/dir1/dir2/gr3/file.ksh" I need to get the /dir1/dir2/gr3 alone. the no of directories may differ at each time. Please advice. thanks in advance. (3 Replies)
Discussion started by: vanathi
3 Replies

9. UNIX for Dummies Questions & Answers

creating a variable

I cannot seem to get the following script to work. I cannot seem to set the variable. What am I missing? bin/bash set -x echo "2" > /tmp/number STATUS='grep -c 2 /temp/number' if ; then echo "Number 2 is found once" else echo "Number 2 is found more or less than one time" fi (3 Replies)
Discussion started by: rexmabry
3 Replies

10. UNIX for Dummies Questions & Answers

Creating alias for directory path

I am trying to create an alias for a frequently used directory path by using alias xyz="/proj/dir_name" and then trying to reach a sub-directoy by using cd xyz/abc but I get an error saying " No such file or directory " plz tell me wats wrong with this ... (3 Replies)
Discussion started by: jasjot31
3 Replies
Login or Register to Ask a Question