Never realized that you could do this until just now:
: >> file
It seems to be functionally similar to:
touch file
Is there a reason why most resources seem to prefer touch over this shell builtin?
Never realized that you could do this until just now:
: >> file
It seems to be functionally similar to:
touch file
Is there a reason why most resources seem to prefer touch over this shell builtin?
You don't even need to use :; you can just > file (at least in bash; other shells may behave differently).
In practical terms, there is no real difference here (though the minuscule overhead of calling out to /bin/touch is a thing).
touch, however, can also be used to modify the timestamps on a file that already exists without changing or erasing the contents; further, > file will blow out any file that already exists. This can be worked around by instead using >> file.
One other difference with touch is that you can have it create (or update the timestamp on) multiple files at once (e.g. touch foo bar baz quux) with a more succinct syntax than with redirection, where each file needs its own redirection (e.g. >foo >bar >baz >quux).
Using touch:
$ touch foo; stat -x foo; sleep 2; touch foo; stat -x foo
File: "foo"
Size: 0 FileType: Regular File
Mode: (0644/-rw-r--r--) Uid: (991148597/redacted) Gid: (1640268302/redacted)
Device: 1,5 Inode: 8597208698 Links: 1
Access: Fri May 25 10:55:19 2018
Modify: Fri May 25 10:55:19 2018
Change: Fri May 25 10:55:19 2018
File: "foo"
Size: 0 FileType: Regular File
Mode: (0644/-rw-r--r--) Uid: (991148597/redacted) Gid: (1640268302/redacted)
Device: 1,5 Inode: 8597208698 Links: 1
Access: Fri May 25 10:55:21 2018
Modify: Fri May 25 10:55:21 2018
Change: Fri May 25 10:55:21 2018
Using redirection:
$ > foo; stat -x foo; sleep 2; >> foo; stat -x foo
File: "foo"
Size: 0 FileType: Regular File
Mode: (0644/-rw-r--r--) Uid: (991148597/redacted) Gid: (1640268302/redacted)
Device: 1,5 Inode: 8597208698 Links: 1
Access: Fri May 25 10:55:21 2018
Modify: Fri May 25 10:56:25 2018
Change: Fri May 25 10:56:25 2018
File: "foo"
Size: 0 FileType: Regular File
Mode: (0644/-rw-r--r--) Uid: (991148597/redacted) Gid: (1640268302/redacted)
Device: 1,5 Inode: 8597208698 Links: 1
Access: Fri May 25 10:55:21 2018
Modify: Fri May 25 10:56:25 2018
Change: Fri May 25 10:56:25 2018
Because you can touch multiple files at one go without typing any extra special characters. That includes stuff like brace expansion, e.g. touch file{1,2,3,4}.
Another issue is that when you're writing a tutorial, it's rather important to realize that your readers probably aren't very well-versed in the subject. A simple command can be much more understandable than some weird-looking combination of non-letter characters. I would expect there are a number of casual shell users who don't know what : is, for the simple reason that it doesn't really do anything. Similarly for a plain > foo without a command: even if you know what a redirection is, a redirection without a source may be unintuitive.
Also, here on unix.se we often write command samples with a leading dollar sign to indicate the prompt. Special characters at the start of the line might be confused with that. (Note that there are systems and shells that use > as part of the default prompt.)
Well, for me, the primary reason is readability. With touch file you know what's going on, even someone not quite educated in shell scripting knows what's going on. And if not, it's easy to do man touch and see this:
A FILE argument that does not exist is created empty
With the cryptic stuff such as : and >, it's more difficult to know what's going on, and as there's no real advantage, there's no need to use that.
Imagine that you are in search of one of your old shell scripts of which you only remember that some marker file in /tmp is being created. It's easy to grep all your *.sh files for the word "touch". Grepping for a colon instead will yield many false positives if you don't know which exact filename to search for.