Skip to main content
remove ksh note per Stephane Chazelas comment
Source Link
phemmer
  • 73.9k
  • 21
  • 199
  • 231

cat /dev/null > file.txt is a useless use of cat.

Basically cat /dev/null simply results in cat outputting nothing. Yes it works, but it's frowned upon by many because it results in invoking an external process that is not necessary.
It's one of those things that is common simply because it's common.

Using just > file.txt will work on most shells, but it's not completely portable. If you want completely portable, the following are good alternatives:

true > file.txt : > file.txt 

Both : and true output no data, and are shell builtins (whereas cat is an external utility), thus they are lighter and more 'proper'.

 

Update:

As tylerl mentioned in his comment, there is also the >| file.txt syntax.

Most shells have a setting which will prevent them from truncating aan existing file via >. You must use >| instead. This is to prevent human error when you really meant to append with >>. I think ksh has this on by default (but it's been years, so I'm not certain). However bash and other shells also support this, it's just off by default. With bash youYou can turn the behavior on with set -o noclobber.
Anyway, with this feature, you have to use >|C to truncate an existing file.

So fromwith this, I think the simplest, most proper, and portable method of truncating a file would be:

:>| file.txt 

cat /dev/null > file.txt is a useless use of cat.

Basically cat /dev/null simply results in cat outputting nothing. Yes it works, but it's frowned upon by many because it results in invoking an external process that is not necessary.
It's one of those things that is common simply because it's common.

Using just > file.txt will work on most shells, but it's not completely portable. If you want completely portable, the following are good alternatives:

true > file.txt : > file.txt 

Both : and true output no data, and are shell builtins (whereas cat is an external utility), thus they are lighter and more 'proper'.

 

Update:

As tylerl mentioned in his comment, there is also the >| file.txt syntax.

Most shells have a setting which will prevent them from truncating a file via >. This is to prevent human error when you really meant to append with >>. I think ksh has this on by default (but it's been years, so I'm not certain). However bash and other shells also support this, it's just off by default. With bash you can turn the behavior on with set -o noclobber.
Anyway, with this feature, you have to use >| to truncate an existing file.

So from this I think the simplest, most proper, and portable method of truncating a file would be:

:>| file.txt 

cat /dev/null > file.txt is a useless use of cat.

Basically cat /dev/null simply results in cat outputting nothing. Yes it works, but it's frowned upon by many because it results in invoking an external process that is not necessary.
It's one of those things that is common simply because it's common.

Using just > file.txt will work on most shells, but it's not completely portable. If you want completely portable, the following are good alternatives:

true > file.txt : > file.txt 

Both : and true output no data, and are shell builtins (whereas cat is an external utility), thus they are lighter and more 'proper'.

 

Update:

As tylerl mentioned in his comment, there is also the >| file.txt syntax.

Most shells have a setting which will prevent them from truncating an existing file via >. You must use >| instead. This is to prevent human error when you really meant to append with >>. You can turn the behavior on with set -C.

So with this, I think the simplest, most proper, and portable method of truncating a file would be:

:>| file.txt 
deleted 20 characters in body
Source Link
phemmer
  • 73.9k
  • 21
  • 199
  • 231

cat /dev/null > file.txt is a useless use of cat.

Basically cat /dev/null simply results in cat outputting nothing. Yes it works, but it's frowned upon by many because it results in invoking an external process that is not necessary.
It's one of those things that is common simply because it's common.

Using just > file.txt will work on most shells, but it's not completely portable. If you want completely portable, the following are good alternatives:

true > file.txt : > file.txt 

Both : and true output no data, and are shell builtins (whereas cat is an external utility), thus they are lighter and more 'proper'.

 

Update:

As tylerl mentioned in his comment, there is also the >| file.txt syntax.

Most shells have a setting which will prevent them from truncating a file via >. This is to prevent human error when you really meant to append with >>. I think ksh has this on by default (but it's been years, so I'm not certain). However bash and other shells also support this, it's just off by default. With bash you can turn the behavior on with set -o noclobber.
Anyway, with this feature, you have to use >| to truncate an existing file.

So from this I think the simplest, most proper, and portable method of truncating a file would be:

:>| file.txt 

cat /dev/null > file.txt is a useless use of cat.

Basically cat /dev/null simply results in cat outputting nothing. Yes it works, but it's frowned upon by many because it results in invoking an external process that is not necessary.
It's one of those things that is common simply because it's common.

Using just > file.txt will work on most shells, but it's not completely portable. If you want completely portable, the following are good alternatives:

true > file.txt : > file.txt 

Both : and true output no data, and are shell builtins (whereas cat is an external utility), thus they are lighter and more 'proper'.

cat /dev/null > file.txt is a useless use of cat.

Basically cat /dev/null simply results in cat outputting nothing. Yes it works, but it's frowned upon by many because it results in invoking an external process that is not necessary.
It's one of those things that is common simply because it's common.

Using just > file.txt will work on most shells, but it's not completely portable. If you want completely portable, the following are good alternatives:

true > file.txt : > file.txt 

Both : and true output no data, and are shell builtins (whereas cat is an external utility), thus they are lighter and more 'proper'.

 

Update:

As tylerl mentioned in his comment, there is also the >| file.txt syntax.

Most shells have a setting which will prevent them from truncating a file via >. This is to prevent human error when you really meant to append with >>. I think ksh has this on by default (but it's been years, so I'm not certain). However bash and other shells also support this, it's just off by default. With bash you can turn the behavior on with set -o noclobber.
Anyway, with this feature, you have to use >| to truncate an existing file.

So from this I think the simplest, most proper, and portable method of truncating a file would be:

:>| file.txt 
deleted 20 characters in body
Source Link
phemmer
  • 73.9k
  • 21
  • 199
  • 231

cat /dev/null > file.txt is a useless use of cat.

Basically cat /dev/null simply results in cat outputting nothing. Yes it works, but it's not necessary. It's frowned upon by many because it results in invoking an external process that is not necessary.
It's one of those things that is common simply because it's common.

Using just > file.txt will work on most shells, but it's not completely portable. If you want completely portable, the following are good alternatives:

true > file.txt : > file.txt 

Both : and true output no data, and are shell builtins (whereas cat is an external utility), thus they are lighter and more 'proper'.

cat /dev/null > file.txt is a useless use of cat.

Basically cat /dev/null simply results in cat outputting nothing. Yes it works, but it's not necessary. It's frowned upon by many because it results in invoking an external process that is not necessary.
It's one of those things that is common simply because it's common.

Using just > file.txt will work on most shells, but it's not completely portable. If you want completely portable, the following are good alternatives:

true > file.txt : > file.txt 

Both : and true output no data, and are shell builtins (whereas cat is an external utility), thus they are lighter and more 'proper'.

cat /dev/null > file.txt is a useless use of cat.

Basically cat /dev/null simply results in cat outputting nothing. Yes it works, but it's frowned upon by many because it results in invoking an external process that is not necessary.
It's one of those things that is common simply because it's common.

Using just > file.txt will work on most shells, but it's not completely portable. If you want completely portable, the following are good alternatives:

true > file.txt : > file.txt 

Both : and true output no data, and are shell builtins (whereas cat is an external utility), thus they are lighter and more 'proper'.

deleted 63 characters in body
Source Link
phemmer
  • 73.9k
  • 21
  • 199
  • 231
Loading
added 61 characters in body
Source Link
phemmer
  • 73.9k
  • 21
  • 199
  • 231
Loading
Source Link
phemmer
  • 73.9k
  • 21
  • 199
  • 231
Loading