Skip to content
shellhacks.dev
Performance

Read a file without cat in Bash

Read a whole file into a variable with $(<file) in Bash. It runs without a cat process and returns the same text, with the trailing newlines removed.

1 min read
Summarize in AI

A script reads a small config file into a variable thousands of times, and $(cat file) forks a process on each one.

Shell
echo "token-value" > secret.txt
a=$(cat secret.txt)
b=$(<secret.txt)
[[ $a == "$b" ]] && echo "same: [$b]"
TIMEFORMAT='%3R seconds'
echo "cat:"
time (for i in {1..5000}; do x=$(cat secret.txt); done)
echo "redirect:"
time (for i in {1..5000}; do x=$(<secret.txt); done)
Text
same: [token-value]
cat:
6.776 seconds
redirect:
0.034 seconds

A command substitution of the form $(<file) is a special case in Bash: the shell reads the file itself, with no cat, as the command substitution section of the manual says. Both forms remove trailing newlines, so the two variables are equal. The timings are for 5,000 reads in a Debian 12 container on an arm64 laptop.

The shortcut is a Bash and ksh feature, and in dash it expands to nothing, so keep $(cat file) in a script with a #!/bin/sh line. Both forms drop trailing newlines, and a NUL byte in the file is dropped with a warning in Bash. For a file with many lines, mapfile -t lines < file is the way to get them into an array.

Share

More snippets

Performance

Count lines in a huge file fast

Count the lines of a big file with wc -l, which is the fastest of the usual tools. It counts newlines, so a last line without a newline is not counted.

2 min read
Performance

Find syscalls that take time

Show how long each system call takes with strace -T. The figure in angle brackets is the time spent inside the call, so a slow write or sleep stands out.

1 min read
Performance

Trace file opens of a command

Find which files a command opens with strace -e trace=openat, and add -e status=failed to see the config files it looked for and did not find.

2 min read

Get new posts by email

One email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.