Reputation: 33
I have come across a shell script with a line which reads as follows
Pfile=/params/tech1.dat:$Pfile;export Pfile
The purpose is to create and export a variable called Pfile
containing value "/params/tech1.dat"
But what is the `:$Pfile`` doing ? Specifically what purpose is the colon serving ?
Have trawled lots of Unix info sources and manuals but cannot find an example which helps explain the above.
Upvotes: 3
Views: 7314
Reputation: 59110
The colon serves as a delimiter on many variables that list paths. It is just a character that is chosen by convention for that purpose.
Here, the piece of code assigns to Pfile
the string that is obtained by evaluating the right hand site, which consists in a constant string /params/tech1.dat:
and a variable $Pfile
. It would perhaps appear more clearly if it were written Pfile="/params/tech1.dat:$Pfile";export Pfile
.
In your specific example, /params/tech1.dat
is prepended to $Pfile
so that, assuming the value of $Pfile
is /other/path
it then becomes /params/tech1.dat:/other/path
. This is understood by many programs as meaning: look in /params/tech1.dat
then in other/path
.
Common examples: PATH, LD_LIBRARY_PATH, LIBRARY_PATH, CPATH, PYTHONPATH
, etc.
If $Pfile
is previously unset or empty, it ends up with a trailing colon: /params/tech1.dat:
which may, or may not, depending on your program, be understood as the working directory (it is the case for the above-listed examples).
Note that :
is a valid character in a path name in many file systems so in the unlikely event that some path contains one, it should probably be escaped.
Finally, note that in other contexts, :
is a Bash function that does nothing.
Upvotes: 3
Reputation: 8839
In this case, you are appending the existing value of $Pfile
to /params/tech1.dat
. If there is no existing value, you will get /params/tech1.dat:
as the value assigned to $Pfile
. :
acts as a delimiter between the two values.
Typically, :
is used as a delimiter for directories in shell variables such as PATH
and LD_LIBRARY_PATH
. Don't know if you have a certain reason to use :
in your variable.
Upvotes: 0
Reputation: 58244
That prepends the path /params/tech1.dat
to whatever Pfile
might already have in it. So Pfile
might already be:
echo $Pfile
/foo/bar:/blah/bleh
Then you do execute the statement:
Pfile=/params/tech1.dat:$Pfile;export Pfile
and you get:
echo $Pfile
/params/tech1.dat:/foo/bar:/blah/bleh
As was mentioned by others, the colon (:
) is typically used as a path or field delimiter.
Upvotes: 2