Howto Quickly Write A Note Without A Text Editor

How to do things, solutions, recipes, tutorials
Post Reply
Message
Author
User avatar
Dougal
Posts: 2502
Joined: Wed 19 Oct 2005, 13:06
Location: Hell more grotesque than any medieval woodcut

Howto Quickly Write A Note Without A Text Editor

#1 Post by Dougal »

If you want to quickly write a few lines to a file, without using mp or such other cli text editors... use a here document:
Just write:

Code: Select all

cat >myfile <<_END
then write whatever you want (including hitting enter) and when you're done just type _END...

For example:

Code: Select all

# cat >myfile <<_END
> bla
> meh
> _END
# cat myfile 
bla
meh
# 
What's the ugliest part of your body?
Some say your nose
Some say your toes
But I think it's your mind

User avatar
rcrsn51
Posts: 13096
Joined: Tue 05 Sep 2006, 13:50
Location: Stratford, Ontario

#2 Post by rcrsn51 »

Or just

Code: Select all

cat > myfile
and press CTRL/D when done.

Laie
Posts: 318
Joined: Sun 20 Jan 2008, 18:42
Location: Germany

#3 Post by Laie »

COOL

User avatar
laika
Posts: 113
Joined: Tue 16 Jan 2007, 00:58

#4 Post by laika »

rcrsn51 wrote:Or just...
But then no orderly > at the beginning of each line :) Seriously, though, here files have some specific application, don't they?

User avatar
Dougal
Posts: 2502
Joined: Wed 19 Oct 2005, 13:06
Location: Hell more grotesque than any medieval woodcut

#5 Post by Dougal »

laika wrote: here files have some specific application, don't they?
They're a tool... you can do what you want with them.
One good usage is if you write a script and want to give a usage/help option, then rather than using echo and having to play with quoting (if your text includes quotes you'll need to escape them etc.), just write the text you want to output and enclose it in a here document.
Another place where they're is where you have a loop that reads from the output of a subshell:
If you pipe the subshell into the loop,

Code: Select all

grep 'bla' file | while read Line ; do
  #do something
done
then the loop is part of the subshell and any variables you set in it will not be remembered.
If the loop reads from a here document

Code: Select all

while read Line ; do
  #do something
done<<_END
$(grep 'bla' filename)
_END
then you're ok...
What's the ugliest part of your body?
Some say your nose
Some say your toes
But I think it's your mind

Post Reply