Sponsored Content
Full Discussion: Creating a PATH variable
Top Forums Shell Programming and Scripting Creating a PATH variable Post 303027892 by bakunin on Thursday 27th of December 2018 05:49:45 PM
Old 12-27-2018
Quote:
Originally Posted by Circuits
So the locations /usr/bin ect are where programs like cp or rm are located. In other words, its similar to importing a library in Python or Java. Once I specify those locations if I call one of the programs it will look there and see if that program exists and if it does it will run the program.
Yes, exactly. You could write a script using the mv, cp, rm, etc.programs, which are all located in /usr/bin (or /bin, depending on your system) by every time specifying the complete path to them. (I suggest to have a look - use the ls command, which is also located there and issue the following command, which will give you a similar output to this:

Code:
$ ls -l /usr/bin
total 145352
-rwxr-xr-x 1 root root          96 Nov 12 15:31 2to3-2.7
-rwxr-xr-x 1 root root       10104 Apr 23  2016 411toppm
-rwxr-xr-x 1 root root          39 Feb  5  2018 7z
-rwxr-xr-x 1 root root          40 Feb  5  2018 7za
-rwxr-xr-x 1 root root          40 Feb  5  2018 7zr
[...]

Somewhere in this list you will see all the programs i mentioned. You could always write /usr/bin/mv to call mv, /usr/bin/cp instead of cp and so on. But you can also include /usr/bin in your PATH variable and if you enter mv the shell (this is the program which takes your input and processes it) will, if it can't find mv, have a look in /usr/bin if it is there - and if it is there (actually it is), then it will use it.

Quote:
Originally Posted by Circuits
I am only wondering because I am attempting to understand what this script is doing. Some parts of it are obvious, other parts are not.
Why don't you just post the script (enclosed in CODE-tags, please)? We can go over it together and answer your questions. Actually what we appreciate most are people willing to learn something for themselves instead of relying on us to do their work. I'd like to understand.... is a highly regarded goal here, infintely higher than please write for me a script which does....

Quote:
Originally Posted by Circuits
However, I find myself wondering why some programs like echo can be called without first specifying a PATH.
This is actually an excellent question! The reason is that not everything you encounter in a script is a "command". There are three (four) distinct types of "things" (for lack of a better word) in a script (apart from such things as variable assignments, calculations and similar things):

1) "reserved words". These are basically what makes for the script language: while...do....done, if...then...else....fi and others are such "reserved words".

2) "built-in commands" or "built-ins", for short: over time it showed that some commands were used so often that the effort needed to load it from external so that it can be executed slowed down considerably the execution of shell scripts. This is why most shells (re-)created these programs inside them so that they could be used without having to load them as external programs. These are "built-ins" because this exactly is what they are: commands but built into the shell already. An example would be the echo command. In fact there is a /usr/bin/echo (or /bin/echo) program you can use but there is also a built-in command echo in most shells. Because built-ins take precedence over external commands you can use the external program by specifying its full path (regardless of what your PATH variable says), but if you use echo without a path then the built-in is used.

3) external commands: These are the programs i told you about before. If you want to use them you either have to specify their full path or put the path were they are located in your PATH variable. Notice that built-ins and reserved words do (for obvious reasons) NOT need any path to be found.

4) aliases: you can create an "alias" for oftenly used commands, even for commands with a certain set of options. For instance when i use the ls command most times i use ls -lai (long form, show hidden files, show the inode number). Since i do not always want to type ls -lai (after all this are seven!!! keystrokes - way too much) i defined an alias:

Code:
alias l='/usr/bin/ls -lai'

and now i can use l and ls -lai would be executed. Think of an alias as something like a macro in other programming languages or a preprocessor statement in C.

You can find out which type the word you want to use is using the type command. In fact this is also an alias (for the bult-in command whence -v), which is built into the shell, so that it is set by default - at least in the shell i use (Korn shell). In the bash shell the type command is a built-in itself. For what whence does in the Korn shell the bash shell relies on the external /usr/bin/whereis program. Here is what using the type command will look like:

Code:
# type while
while is a keyword

# type whereis
whereis is a tracked alias for /usr/bin/whereis

# type echo
echo is a shell builtin

# type /bin/echo
/bin/echo is /bin/echo

I suggest you try yourself and play around yourself to get acquainted.

I hope this helps.

bakunin

Last edited by bakunin; 12-28-2018 at 06:00 AM..
These 2 Users Gave Thanks to bakunin For This Post:
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 06:48 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy