Operating System - HP-UX
1834296 Members
2236 Online
110066 Solutions
New Discussion

what's mean of ":" in shell script?

 
SOLVED
Go to solution
常有慈悲心
Regular Advisor

what's mean of ":" in shell script?

i find there has some ":" in shell script
and in /sbin/init.d/template,
what's mean of ":"?
7 REPLIES 7
Sanjay Kumar Suri
Honored Contributor

Re: what's mean of ":" in shell script?

Interesting observation.

It seems telling that to add code in place of ':'.

It seems to acting like a comment.

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
Sridhar Bhaskarla
Honored Contributor

Re: what's mean of ":" in shell script?

Hi,

It does nothing other than returning an exit code of 0. Try the following script with and without :. to see the difference. It is usually used as a 'placeholder' for adding more script.

if [ -f /etc/nofile.out ]
then
echo "nofile is there"
else
:
fi

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
D Block 2
Respected Contributor

Re: what's mean of ":" in shell script?

I think the ":" or colon in standard AT&T shell scripting means: NoOP.
or you can think of this like a place-holder in you code.

if [ ! -f myfile ]
:
# this is a comment
else
echo "the file myfile is valid"
fi
Golf is a Good Walk Spoiled, Mark Twain.
Geoff Wild
Honored Contributor

Re: what's mean of ":" in shell script?

It is also used in funtions as a place holder - if you have a function - but don't want to execute anything (say you are testing), or for example, you don't want an application to start...

Example, in MC/SG, sometimes, I only want to mount the file systems in a package - but not start up SAP:

{
# ADD customer defined run commands.
: # do nothing instruction, because a function must contain some command.
# /etc/cmcluster/PRODDBCI/sapdbci.cntl startDBCI PROD
test_return 51
}


If I didn't have the : then the script would fail...


Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Michael Roberts_3
Honored Contributor
Solution

Re: what's mean of ":" in shell script?

It is the 'no-op' function. an alias for ':' is
'true'.

while :
do
ls
sleep 3
done

equivalent to

while true
do
ls
sleep 3
done
etouq ot hguone revelc ton m'i
Bill Hassell
Honored Contributor

Re: what's mean of ":" in shell script?

As mentioned, it is a placeholder. In some scripts, you might want a particular action to be nothing (don't do anything) but the script syntax requires at least one action. For example, here is the famous do-nothing script:

while :
do
:
done

This script loops around as fast as possible, doing nothing useful and consuming 100% of one CPU.

Now in the template file, the line says:

# Execute the commands to start your subsystem (replace :)
:
set_return

So you take out the : and put your command(s) on that line. Without the :, there would be: else fi, and you'll get the famous: "`fi' is not expected." error message.


Bill Hassell, sysadmin
Michael Roberts_3
Honored Contributor

Re: what's mean of ":" in shell script?

Another use of ':' is to set defaults if
a variable isn't already set.

: ${somevar:="default"}
print ${somevar}
default

without the ':' you get
${somevar:="default"}
/usr/bin/sh: default: Execute permission denied.
etouq ot hguone revelc ton m'i