Find the answer to your Linux question:
Results 1 to 2 of 2
I'm having trouble understanding what the colon is used for in bash? Can somebody explain it to me with an example? thanks!...
  1. #1
    Just Joined!
    Join Date
    Jan 2009
    Posts
    1

    the meaning of builtin colon

    I'm having trouble understanding what the colon is used for in bash? Can somebody explain it to me with an example? thanks!

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Well first, from the bash man page:
    Code:
           : [arguments]
                  No  effect;  the command does nothing beyond expanding arguments
                  and performing any specified redirections.  A zero exit code  is
                  returned.
    Basically, : does nothing, and produces no output. So why is it useful? It allows us to do stuff like this:
    Code:
    #!/bin/bash
    
    if [ $DEBUG_DEFINED ]; then
        DEBUG_COMMAND=echo
    else
        DEBUG_COMMAND=:
    fi
    
    DEBUG_COMMAND "This is a debug statement."
    Now, we can enable or disable debug statements just by changing the value of $DEBUG_DEFINED.

    : is also useful for clearing files:
    Code:
    : > file
    file still exists, but it has no content.

    Does this make sense?
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...