Unix shell script - purpose of colon

shell, unix

Solution

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.

Problem

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.

Original source