Skip to content
Search

Cat

lastmod 2026-02-06created 2015-10-2014 edits

The cat command can print the contents of a text file, or concatenate two or more files and print them.

Example

Print with a number on each line:

$ cat -b test.txt

Number blank lines too:

$ cat -n test.txt

Print two or more consecutive blank lines as a single line:

cat -s test.txt

Assign multi-line string

Examples of cat <<EOF syntax usage in Bash. For reference, this technique is called a Here document. See that page for details.

Assign multi-line string to a shell variable

$ sql=$(cat <<EOF
SELECT foo, bar FROM db
WHERE foo='baz'
EOF
)

The $sql variable now holds the new-line characters too. You can verify with echo -e "$sql"

Pass multi-line string to a file in Bash

$ cat <<EOF > print.sh
#!/bin/bash
echo \$PWD
echo $PWD
EOF

The print.sh file now contains:

#!/bin/bash
echo $PWD
echo /home/user

Pass multi-line string to a pipe in Bash

$ cat <<EOF | grep 'b' | tee b.txt
foo
bar
baz
EOF

The b.txt file contains bar and baz lines. The same output is printed to stdout.

To prevent environment variable substitution

Used as follows, $temp is substituted as an environment variable.

$ cat << EOF > tee b.txt
$temp
EOF

To prevent this, quote EOF with single quotes ('EOF').

$ cat << 'EOF' > tee b.txt
$temp
EOF

Text Processing Commands

List of text processing commands

Text

Common

grep, sed, awk, cat, xargs, rev, tee, ack, tr, stdbuf, split

Diff

diff, patch

Cutting

cut, head, tail, less, more, split, watch

Sort

uniq, sort

Hash

base64, md5sum, sha1sum, sha224sum, sha256sum, sha384sum, sha512sum

Clipboard

clip (Windows), pbcopy (Mac OSX), pbpaste (Mac OSX), xclip (Debian)

E.T.C

regexp, parallel

See also

Favorite site

author Zer0 sitemap