home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 45.8 Handling Signals to Child Processes Chapter 45
Shell Programming for the Initiated
Next: 45.10 Removing a File Once It's Opened - for Security and Easy Cleanup
 

45.9 The Unappreciated Bourne Shell ":" Operator

Some people think that the Bourne shell's : is a comment character. It isn't, really. It evaluates its arguments and returns a zero exit status (44.7 ) . Here are a few places to use it:

  • Replace the UNIX true command to make an endless while loop (44.10 ) . This is more efficient because the shell doesn't have to start a new process each time around the loop (as it does when you use while true ):

    while :
    do
       commands
    
    
    done

    (Of course, one of the commands will probably be break , to end the loop eventually.)

  • When you want to use the else in an if (44.8 ) , but leave the then empty, the : makes a nice "do-nothing" place filler:

    if something
    
    
    then :
    else
       commands
    
    
    fi

  • If your Bourne shell doesn't have a true # comment character, you can use : to "fake it." It's safest to use quotes so the shell won't try to interpret characters like > or | in your "comment":

    : 'read answer and branch if < 3 or > 6'

  • Finally, it's useful with parameter substitution (45.12 ) like ${ var ?} or ${ var = default } . For instance, using this line in your script will print an error and exit if either the USER or HOME variables aren't set:

    : ${USER?} ${HOME?}

- JP


Previous: 45.8 Handling Signals to Child Processes UNIX Power Tools Next: 45.10 Removing a File Once It's Opened - for Security and Easy Cleanup
45.8 Handling Signals to Child Processes Book Index 45.10 Removing a File Once It's Opened - for Security and Easy Cleanup

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System