cat

Concatenates files. Often used to display the contents of a file. Often used to display the contents of a file, or to create a multi-line file, such as a hea document.

NAME
     cat -- concatenate and print files

SYNOPSIS
     cat [-benstuv] [file ...]

github

  • The file in jupyter notebook format on github is here .

google colaboratory

  • To run it in google colaboratory here cat_nb.ipynb)

environment

The author’s OS is macOS, and the options are different from those of Linux and Unix commands.

When you actually run the command, you will need to use the prefix ! and %%bash in the first line.

!sw_vers
ProductName: Mac OS X
ProductVersion: 10.14.6
BuildVersion: 18G95
!bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.

Usage examples

Display and concatenate files

cat file
cat file1 file2
cat file1 file2 > file3

Creates a file and displays its contents.

%%bash
echo "a b c" > temp.txt
cat temp.txt
a b c

Create two files and concatenate them.

%%bash
echo "e f g" > temp1.txt
echo "h i j" > temp2.txt
cat temp1.txt temp2.txt > temp3.txt

You can see that temp3.txt is created, and temp1.txt and temp2.txt are concatenated in it.

%%bash
cat temp3.txt
e f g
h i j

Creating a Heard Document

This is often used to create multi-line files in scripts. The EOF notation can be anything you want. Redirects the file when it is created.

%%bash

cat << EOF > temp10.txt
a b c
e f g
h i j
EOF
cat temp10.txt
a b c
e f g
h i j

However, this does not allow you to include the command as is. The result of the command, variables, etc. will be expanded and displayed.

%%bash

cat << EOF > temp11.sh
#! /bin/bash

user="test"

echo ${user}

EOF

If you don’t want to expand the variables here, or if you want to keep the notation of the command itself, enclose EOF in single quotation marks.

%%bash
cat << 'EOF' > temp12.sh
#! /bin/bash

user="test"

echo ${user}

EOF

cat temp12.sh
#! /bin/bash

user="test"

echo ${user}

and you can see that ${user} is properly listed in the file without being expanded.

Typical options

  • t : Display tabs explicitly (^I).
  • e : Print newline code explicitly (displayed as $)
%%bash
echo -e "a\tb\tc" > temp2.txt
cat -t temp2.txt
a^Ib^Ic
%%bash
echo -e "a\tb\tc" > temp3.txt
cat -e temp3.txt
a b c$