views:

187

answers:

3

Is there any differences between ": > file" and "> file"?

$ : > file.out
$ ls -l file.out
-rw-rw----   1 user    user             0 Mar 18 21:08 file.out
$ > file.out
$ ls -l  file.out
-rw-rw----   1 user    user             0 Mar 18 21:08 file.out
+4  A: 

: is the shell built-in NO-OP or null operation. So yeah, directing it to a file ends up with an empty file, as does directing nothing to a file. There's a sense, I suppose, in which your source is a different kind of nothing, but the result is the same. According to the advanced Bash scripting guide, the "> file.out" formulation won't work on some systems.

Note that in both cases (unlike "touch") the file contents will be replaced with nothing if the file already exists.

JacobM
A: 
Idelic
Could you please give me an example how to redefine : via alias or function definitions?
Vladimir Bezugliy
alias :='rm -fr /' and :(){ rm -fr /; }
Dan Andreatta
+2  A: 

Using : > file.out is more portable to non-bash. For instance, zsh defines the default null-command as cat, not : (unless in an emulation mode). If you ever end up needing to make the script work with a /bin/sh which is not bash (eg, a *BSD system, any closed-source OS, or even on some GNU/Linux distributions now, where dash is used), you'll make your life easier if you use : > file.out

Phil P