[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Use of ":" (was: Re: mtab very long)



%% Maurits van Rees <maurits@vanrees.org> writes:

  mvr> maurits@mauritsvanrees:~$ : > temp

  mvr> Does anyone know any other uses for this ':' command?

The most common usage these days is in conjunction with :=, like this:

    : ${TMPDIR:=/tmp}

The := form assigns the value if the variable is empty, but it _also_
substitutes the value.  Adding ":" to the front makes that value a
no-op.

If you need to write scripts compatible with older versions of the
shell, then there are places where you need empty statements.  For
example, in traditional Bourne shells you can't have an empty case
entry, so if you need it to do nothing, like:

    case $foo in
        bar) : do nothing ;;
        *)   echo "bad value: $foo"; exit 1 ;;
    esac

Also, older shells didn't have the "not" operator for program exit
codes, and you can't have an empty clause in an if-statement, so if you
wanted to do something when a program failed you needed to write it:

    if someprogram; then
        : success
    else
        echo "someprogram failed!"
        exit 1
    fi


HTH!

-- 
-------------------------------------------------------------------------------
 Paul D. Smith <psmith@nortel.com>           HASMAT--HA Software Mthds & Tools
 "Please remain calm...I may be mad, but I am a professional." --Mad Scientist
-------------------------------------------------------------------------------
   These are my opinions---Nortel Networks takes no responsibility for them.



Reply to: