":" operator???

Hi,
this is the first time that i write here.
I'm learning bash script about 10 day.

I don't understand something like this

: ${nomeutente=�whoami�}

or this

: > data.xxx

In this sintax, what does it mean the character :?
Is it an operator? ... What is it?

Thank you

It is the "null" shell builtin. It does nothing but unlike a comment "#", it processes redirections and expands parameters.

True, but the question is why would you do such a thing?

The answer is usually portability. If your script needs to ">file", that alone won't work everywhere and neither will "echo -n > file" or "echo "\c". That ": >file" will work.

---------- Post updated at 11:06 AM ---------- Previous update was at 09:30 AM ----------

Oh, and the other thing:

: ${nomeutente=�whoami�}

is because a bare

${nomeutente=�whoami�}

would try to execute the result. So if "whoami" is "ls", then "ls" would run. If it's "azrtyuw" it would also try to run and you'd get an error.

In this particular case you could just do

n=`whoami`

but consider

: ${nomeutente:=�whoami�}

(sets nomeutente only if not already set)

You'd need if-then or a case block otherwise which is much longer and possibly confusing.

Empty commands do indeed clash with csh and behave differently with zsh.
Any command with no output can be used as a portable workaround like:

sleep 0 > file
printf "" > file
true > file 
test > file

But

: > file

definitely wins in term of both conciseness and being puzzling.

A more commonly found use for :

while :
do
  #  code goes here - that you want to do forever, or until ctrl/c
done

I prefer

while true

which is more readable.

Ahem, ... this is not entirely correct. In ksh there is a special operation for variables to give them values if they are not defined already:

variable=${variable:-"DEFAULT"}

This will set "$variable" to "DEFAULT" if it is not set and leave it unchanged, if it is already. Unlike the ": command ..."-construct this is entirely readable and can - even if someone doesn't know this - be found in every ksh man page.

bakunin