":" operator???


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ":" operator???
# 1  
Old 03-29-2010
":" 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
Code:
: ${nomeutente=‘whoami‘}

or this
Code:
: > data.xxx

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

Thank you

Last edited by zaxxon; 03-29-2010 at 09:08 AM.. Reason: use code tags please, ty
# 2  
Old 03-29-2010
It is the "null" shell builtin. It does nothing but unlike a comment "#", it processes redirections and expands parameters.
# 3  
Old 03-29-2010
Quote:
Originally Posted by jlliagre
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.
# 4  
Old 03-29-2010
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:
Code:
sleep 0 > file
printf "" > file
true > file 
test > file

But
Code:
: > file

definitely wins in term of both conciseness and being puzzling.
# 5  
Old 03-29-2010
A more commonly found use for :
Code:
while :
do
  #  code goes here - that you want to do forever, or until ctrl/c
done

# 6  
Old 03-29-2010
I prefer
Code:
while true

which is more readable.
# 7  
Old 03-29-2010
Quote:
Originally Posted by TonyLawrence
: ${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.
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:

Code:
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

perl's substitution operator "s" with file contents?

Please show me how to make substitution over the contents of a file in a perl script. In a perl script, the core part of substitution operation is s/FINDPATTERN/REPLACEPATTERN/g; However, I cannot figure out how to make the substitution occur over the contents of a file. The following... (3 Replies)
Discussion started by: LessNux
3 Replies

3. UNIX for Dummies Questions & Answers

"binary operator exected" problem

Hey guys/gals, Am having trouble getting an 'if statement' to play nice.. I am playing with a script and need 'if' to check whether "y" or "Y" has been entered and if not to exit. In previous scripts I was playing with there would normally be a Y and a N response possibility so each... (2 Replies)
Discussion started by: TAPE
2 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Shell Programming and Scripting

What "-a" operator means in "if" statement

Hi I am trying to figure out what the following line does, I work in ksh88: ] && LIST="$big $LIST" Not sure what "-a" means in that case. Thanks a lot for any advice -A (1 Reply)
Discussion started by: aoussenko
1 Replies

6. Shell Programming and Scripting

cannot properly employ "or" operator in an if statement (bash)

Hi, I have a variable, $sername, and I would like to display this variable only if it *does not* contain either of these two tags: *DTI*FA* or *DIFF*FA*. I think the syntax for my 'or' operator is off. The variable $sername is continuously changing in an outer loop (not shown), but at the... (4 Replies)
Discussion started by: goodbenito
4 Replies

7. Shell Programming and Scripting

error "test: [-d: unary operator expected" very confused.

Im trying to check if a series of directory exists and if not create them, and am having issues. All the instances of test return with the error "test: #!/bin/bash location_Parent=~/Documents/sight_of_sound location_IMG=~/Documents/Sight_of_sound/IMG location_AUD=~/Documents/Sight_of_sound/AUD... (4 Replies)
Discussion started by: orionrush
4 Replies

8. Shell Programming and Scripting

How can I use a pipe operator in a variable: OPTION="| command"?

I have a string of commands I am piping some data through and I want to allow command line switches to select which commands are used. I want to do something like this: OPTION="| command3" command1 -a -b c.txt | command2 -d -e $OPTION >result.txt I want to do it that way because OPTION may be... (1 Reply)
Discussion started by: KenJackson
1 Replies

9. Shell Programming and Scripting

if returns "unknown test operator"

Greetings, using ksh on Solaris, I am trying to identify the current version of a package installed on multiple servers using if statement in a precursor to upgrading. I have searched the forums and have found many hits, reviewed 3 pages and have tried the different variations noted there. Also... (3 Replies)
Discussion started by: 22blaze
3 Replies

10. Shell Programming and Scripting

awk returning "[: ==: unary operator expected"

Hi All, I am new to shell scripting and right now I am just limited to using the pre-written scripts. I am in to Infrastructure management where we use different scripts to get the information passed on to the monitoring tools. I am trying to use this script to get the information about the... (2 Replies)
Discussion started by: theamrit
2 Replies
Login or Register to Ask a Question