Chani
Chani

Reputation: 780

do I need cat to write a heredoc to a file?

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...

Upvotes: 10

Views: 3586

Answers (4)

Livven
Livven

Reputation: 8199

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.

Upvotes: 8

cmh
cmh

Reputation: 10937

In zsh it is a UUOC because:

>myfile <<EOF
some lines
and more lines
EOF

Works fine.

Upvotes: 2

jgr
jgr

Reputation: 3974

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.

Upvotes: 2

jim mcnamara
jim mcnamara

Reputation: 16389

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

Upvotes: 2

Related Questions