[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] What is : good for
From: |
Greg Wooledge |
Subject: |
Re: [Help-bash] What is : good for |
Date: |
Fri, 19 Feb 2016 10:02:43 -0500 |
User-agent: |
Mutt/1.4.2.3i |
On Fri, Feb 19, 2016 at 12:27:08AM -0500, David Niklas wrote:
> I tried a couple of things with it, but I can't think of a use.
It's used any place the grammar requires a command but you don't actually
want to do anything.
There are three places it is traditionally used.
1) In the Bourne shell, which had no ! prefix, it was used with "if":
if something
then
:
else
stuff
fi
In Bash, we'd simply write:
if ! something; then
stuff
fi
2) It is used with the "assign a default value" parameter expansion:
: "${foovar:=foodefault}"
3) In a "case" branch whose only existence is an exclusion from a later
branch:
case $thing in
x*y*) : ;;
x*) die "Error: x without y" ;;
esac
It can also be used in place of "true", when you want to loop for a
really long time. For example:
while ! thing you are polling for; do
:
done
while :; do
something
something else
some check && break
done
I'd use "while true; do" in the last one, but some people like : more.