help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] What is : good for


From: Bob Proulx
Subject: Re: [Help-bash] What is : good for
Date: Sat, 20 Feb 2016 11:54:46 -0700
User-agent: Mutt/1.5.24 (2015-08-30)

Greg Wooledge wrote:
> David Niklas wrote:
> > I tried a couple of things with it, but I can't think of a use.
> 
> It's used any place the grammar requires a command but you don't actually
> want to do anything.
> 
> There are three places it is traditionally used.

Additionally since ":" is a command then it is traced when -x is in
action.  Therefore I like to use it as a conditional debug print
statement.  Here is an on-the-fly created contrived example that may
be too abreviated to actually show how well it can work.

  #!/bin/sh
  var=$1
  case $var in
    BAR) echo "doing bar stuff" ;;
    FOO) echo "doing foo stuff" ;;
  esac
  exit 0

Let's say the script isn't acting as you expect.  You decide to trace
the script with -x to see what commands are run.

  + var=F00
  + exit 0

Let's assume that the var=$1 is far away in the script and not so
immediately obvious as it is here in this very short example.  I am
going to redact the var= assignment to show what we might see there.

  + exit 0

Hmm... Not as helpful as it could be since 'case' is a shell keyword
and the control structure completely internal the case statement isn't
shown at all.  However I want to see some of that "hidden"
information.  In this case I can use ":" as a conditionally printed
debug statement.

  #!/bin/sh
  var=$1
  : "debug: var=$var"
  case $var in
    BAR) echo "doing bar stuff" ;;
    FOO) echo "doing foo stuff" ;;
  esac
  exit 0

With that in place the -x result is:

  + : debug: var=F00
  + exit 0

Obviously F00 isn't FOO and this tells me what is processed by the
case statement which would otherwise be invisible.  Changing that in
my contrived debug example would show the desired result.

  + : debug: var=FOO
  + echo doing foo stuff
  doing foo stuff
  + exit 0

Those statements I leave in place in production and that way when run
without -x tracing enabled then the statements are not shown be are
always available later for debugging with -x.  I insert these whenever
visibility is desired for tracing any invisible detail.  Don't overuse
them however.  Small is beautiful.

Since ":" is internal to the shell these short statements execute very
fast and won't be a performance degradation.  If you think it is a
performance issue then don't use a shell script at all but write the
entire task in C.

Note that using ":" like this is the same as using 'true' but more
aesthetically pleasing.  Also note that ":" is a command that is
executed and therefore arguments must be quoted properly.  Don't say
something like this:

  : This isn't a comment and shell quotes are interpreted
  : Saying 5 > 3 will cause problems with redirections
  : Saying * will match all files in the current directory
  : Saying ** might match a very large number of files in some shells.
  : "Therefore always quote for valid arguments."

Which is why arguments being traced must be valid command line
arguments and shell metacharacters handled.  Always quote properly.

Bob



reply via email to

[Prev in Thread] Current Thread [Next in Thread]