LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-16-2009, 09:13 AM   #1
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,293

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Single colon by itself in a shellscript?


Hi

I just made an init.d script on a Debian server for a daemon. I copied /etc/init.d/skeleton and edited my copy. It works fine. But there was one thing I could not understand. On the very end of the script, there is a line with a single : character.

I tried to figure out what it does, and I found this:

Quote:
: (a colon)

: [arguments]

Do nothing beyond expanding arguments and performing redirections.
The return status is zero.
From http://www.gnu.org/software/bash/man...-Builtins.html

Why end a script with a : character? I tried entering : as a command, and nothing happens.

This is not very important, just a bit curious about what it means.
 
Old 06-16-2009, 09:18 AM   #2
grepmasterd
Member
 
Registered: Aug 2003
Location: Seattle
Distribution: ubuntu, lately
Posts: 182
Blog Entries: 1

Rep: Reputation: 35
':' means 'do nothing'

it's usually used in the context of an 'if' statement when you are more interested in the 'else' case:

Code:
if true ; then 
   :
else
   # do something useful here
fi
I'm not sure of the usefulness of putting ':' at the end of a script except to set the exit value to 0. maybe that is all that was intended
 
Old 06-16-2009, 09:53 AM   #3
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,293

Original Poster
Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Ok. I think I understand. It's a way of setting the exit value to 0 without actually exiting. But I don't think it's very readable, using "exit 0" is a lot easier to understand.
 
Old 06-16-2009, 10:25 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,421

Rep: Reputation: 200Reputation: 200Reputation: 200
man bash
Code:
    : [arguments]
              No  effect;  the command does nothing beyond expanding arguments
              and performing any specified redirections.  A zero exit code  is
              returned.
yes it's useful in an if as above, it helps to make
more readable code sometimes, as well as leaving a placeholder for
later.

I use it sometimes to test or set variables, as it works
even when you have set -o nounset in ksh and bash
and also it doesn't evaluate to a command so that it doesn't need to
be in context of an if or echo or so on.

Code:
: ${NON_VAR:?Error: NON_VAR should be blah blah lah}
usually only when I'm doing a very thorough, production strength shell script.

Last edited by bigearsbilly; 06-16-2009 at 10:26 AM.
 
Old 06-16-2009, 12:03 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 110Reputation: 110
As a side note to this: in bash a colon actually can (be made to) do something:
Code:
bash:~$ :(){ echo doing something; }
bash:~$ :
doing something
bash:~$ :;:;:;:
doing something
doing something
doing something
doing something
 
Old 06-16-2009, 12:07 PM   #6
grepmasterd
Member
 
Registered: Aug 2003
Location: Seattle
Distribution: ubuntu, lately
Posts: 182
Blog Entries: 1

Rep: Reputation: 35
as can anything

Code:
bash:~$ unalias ls
bash:~$ ls(){ echo "sorry I really don't feel like that right now" ; }
bash:~$ ls
sorry I really don't feel like that right now
 
Old 06-16-2009, 12:31 PM   #7
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
See http://en.wikipedia.org/wiki/Fork_bomb.

Don't run the following:
Code:
:(){ :|:& };:

Last edited by pwc101; 06-16-2009 at 12:32 PM.
 
Old 06-16-2009, 01:06 PM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,421

Rep: Reputation: 200Reputation: 200Reputation: 200
Quote:
Originally Posted by Guttorm View Post
Ok. I think I understand. It's a way of setting the exit value to 0 without actually exiting. But I don't think it's very readable, using "exit 0" is a lot easier to understand.
no it's not an exit 0
it just evaluates nothing and sets the $? to 0.
 
Old 06-16-2009, 03:29 PM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,421

Rep: Reputation: 200Reputation: 200Reputation: 200
Quote:
Originally Posted by pwc101 View Post
See http://en.wikipedia.org/wiki/Fork_bomb.

Don't run the following:
Code:
:(){ :|:& };:


d'oh!
 
Old 06-17-2009, 03:30 AM   #10
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
It's also used for infinite while loops:

Code:
while :
do

done
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Colon in version number (debian) kozuch82 Linux - Newbie 1 11-13-2008 01:02 PM
[SOLVED] scp file name with colon in it tuxstar Linux - Newbie 2 09-03-2007 08:38 AM
BASH: Purpose of the : (colon) vrillusions Programming 4 12-29-2005 01:27 PM
AIX colon files rayudu AIX 2 07-09-2005 05:51 PM
need a semi colon .. but vert spooge Linux - General 4 09-19-2004 03:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration