10

I've got a script that writes to a file, like so:

cat >myfile <<EOF
some lines
and more lines
EOF

but I'm not sure whether this is a Useless Use of Cat or not...

Facundo Casco
  • 10,065
  • 8
  • 42
  • 63
Chani
  • 760
  • 7
  • 11

4 Answers4

8

Even if this may not be a UUOC, it might be useful to use tee instead:

tee myfile <<EOF
some lines
and more lines
EOF

It's more concise, plus unlike the redirect operator it can be combined with sudo if you need to write to files with root permissions.

Livven
  • 7,841
  • 6
  • 25
  • 20
  • Doesn't this also write to standard out? In most cases this is not desired. – Bogdan Calmac Aug 10 '15 at 21:42
  • True – if you don't want that, add `> dev/null` to the end of the first line. See also [this comment](http://stackoverflow.com/questions/2953081/how-can-i-write-a-here-doc-to-a-file-in-bash-script/17093489#comment24896950_17093489). – Livven Aug 11 '15 at 16:06
2

It is not really a UUOC. You can also do the same with echo:

echo "this is line
this is another line
this is the last line" > somefile
jim mcnamara
  • 16,005
  • 2
  • 34
  • 51
2

UUOC is when you use cat when it is not needed. As in:

cat file | grep "something"

Instead you can do it whithout cat:

grep "something" file

Look here for the original definition of UUOC.

jgr
  • 3,914
  • 2
  • 24
  • 28
2

In zsh it is a UUOC because:

>myfile <<EOF
some lines
and more lines
EOF

Works fine.

cmh
  • 10,612
  • 5
  • 30
  • 40