bash ‘cat’ - the poor man’s text editor

Not many people realize that on bash you do not necessarily need a full blown text editor like vim, emacs or nano, or need to remember the heredoc logic to write text on the command line: All you need is some “cat magic” Here is how!

Olaf Schmalfuß https://www.datamercs.net
2017-09-11

// cat: concatenate and WRITE files

Not many people realize that on bash you do not necessarily need a full blown text editor like vim, emacs or nano, or need to remember the heredoc logic to write multi-line text on the command line, but that for most cases good ol’ cat is all you needed.

All you need to do is cat into a(n empty) file and close with CTRL+d

One can add some extra text with >> again


$ cat > text.txt
The Lord of the Rings
is one of those things
if you like it you do

# CTRL+d to finish

# let's add some more text to the same file
$ cat >> text.txt
if you don't then you boo

# CTRL+d to finish

# result:
$ cat text.txt
The Lord of the Rings
is one of those things
if you like it you do
if you don't then you boo


# same in heredoc logic
cat << EOF > text.txt
The Lord of the Rings
is one of those things
if you like it you do
if you don't then you boo
EOF

Citation

For attribution, please cite this work as

Schmalfuß (2017, Sept. 11). OS DataMercs: bash 'cat' - the poor man's text editor. Retrieved from https://www.datamercs.net/posts/2017-09-11-bash-cat-the-poor-mans-text-editor/

BibTeX citation

@misc{schmalfuß2017bash,
  author = {Schmalfuß, Olaf},
  title = {OS DataMercs: bash 'cat' - the poor man's text editor},
  url = {https://www.datamercs.net/posts/2017-09-11-bash-cat-the-poor-mans-text-editor/},
  year = {2017}
}