Wednesday, October 13, 2010

Something new every day: Bourne Shell variables

I've worked with the Unix Operating System and its variants since the late 1980s. I've worked with the Bourne Again Shell (bash) since the early 1990s. And yet today I learned something new about variable expansion. In the startup scripts for a source code indexing system called OpenGrok, I found this gem:
somecommand ${PROG:+-c} ${PROG}
Now, I know that ${FOO-bar} will be replaced with the value of $FOO if it is currently set or "bar" if it's not. That much I learned many years ago, but this usage of "+" was new to me. After some testing, I found that "+" substitutes the following text if and only if the variable is set, otherwise it substitutes nothing. Thus if $PROG were set to "foo", the above text would execute:
somecommand -c foo
But if $PROG were not set, then somecommand would be run with no arguments at all. Very slick!

How I managed to go over 20 years without learning that, I'm unsure (then again, perhaps I've learned and forgotten it...)

4 comments:

  1. Great, well! Interestingly written review. Using business solutions it is the best way to receive software development.

    ReplyDelete
  2. A lot of thanks for your fresh update. Casino affiliates always look for casino affiliate programs to increase their revenue income from best casinos or poker rooms.

    ReplyDelete
  3. Thanks for sharing with us great info. For auto owners who is looking for new cars, compare autoquotes from top auto insurance companies.

    ReplyDelete