Mastering Input Redirection in Linux - <, <<, and <<<

Mastering Input Redirection in Linux - <, <<, and <<<

- 4 mins

Introduction

In the Linux shell, redirection operators let you control how commands receive input and where they send output. Most users know about > and << for writing to files, but input redirection (<, <<, and <<<) is equally powerful.

These three operators allow you to feed data into commands directly from files, inline text, or strings. Mastering them will help you write cleaner scripts and simplify everyday sysadmin tasks.

1. < – Redirect Input from a File

Example 1. < – Redirect Input from a File

The < operator takes a file’s contents and provides it as standard input (stdin) to a command.

Example 1 – Count lines in a file:

wc -l < /var/log/dpkg.log

redirect

This tells wc to count lines by reading /var/log/dpkg.log as input, without using cat.

Equivalent to:

cat /var/log/dpkg.log | wc -l

Example 2 – Feed SQL file into MySQL:

mysql -u root -p < database_backup.sql

This imports all SQL statements in the file directly into the database.


Example 3 – Compare two files line by line:

diff <(sort file1.txt) <(sort file2.txt)

redirect

Here <() (process substitution) is combined with < to pass sorted versions of files into diff.


2. << – Here Document (Multi-Line Input)

The << operator lets you supply multi-line input inline. It continues until a specified delimiter is reached. This is very common in shell scripts.

Example 1 – Pass multiple lines to cat:

cat <<EOF
Hi
Hello World
This is a here document.
EOF

redirect

Example 2 – Automating database queries:

mysql -u root -p <<SQL
USE employees;
SELECT COUNT(*) FROM users;
SELECT * FROM logs LIMIT 3;
SQL

Here, everything between «SQL and the closing SQL is passed to the mysql client.

Example 3 – Configure files inline:

tee /etc/nginx/conf.d/test.conf <<CONFIG
server {
    listen 8080;
    server_name example.com;
    root /var/www/html;
}
CONFIG

This writes an Nginx config file directly from the shell without editing manually.

Example 4 – Run multiple commands over SSH:

ssh admin@server <<'COMMANDS'
cd /var/log
ls -lh
df -h
uptime
COMMANDS

The block of commands runs remotely as if typed one by one.

redirect

3. <<< – Here String (Single-Line Input)

The <<< operator is like «, but for single strings. It’s perfect for quick one-liners.

Example 1 – Count words in a string:

wc -w <<< "Hi my name is guney"

redirect

Example 2 – Grep a string:

Example:

grep "root" <<< "user:root:1234"

Example 3 – Pass variables without echo:

read name <<< "Guney"
echo "Hello, $name"

![redirect][6]

4. Practical Scenarios for Sysadmins and Developers

Database administration

Configuration management

Testing commands

Remote automation

Scripting best practices


5. Summary

These operators make your Bash scripts shorter, cleaner, and more powerful. Once you adopt them, you’ll stop overusing cat and echo, and your automation tasks will look much more professional.


Thanks for reading!

Guneycan Sanli

Guneycan Sanli

Guneycan Sanli

A person who like learning, music, travelling and sports.

comments powered by Disqus