Here Strings with <<<
A here string gives a command a single string as its standard input. It is written with three less-than signs, <<<, followed by the text. It is a Bash feature (also found in Zsh and a few other shells) that replaces the common but clumsy pattern echo "text" | command with a form that starts no extra process and does not run the receiving command in a subshell.
Understanding the Concept
Lesson 63 showed that a here document sends a block of lines to a command. A here string is the one-line version. You do not need a delimiter or a closing marker: the word after <<< is the entire input. The shell expands it, adds a newline at the end, and attaches it to the standard input of the command.
Syntax
command <<< "string"
The string is usually quoted. A variable can stand in for the string: command <<< "$variable".
Basic Example
cat <<< "hello"
tr 'a-z' 'A-Z' <<< "hello"
Output:
hello
HELLO
Understanding the Example
The first command printed the string, because cat copies its standard input to its standard output. The second one gave the same string to tr, which converted lowercase letters to uppercase. In both cases the string was the input of the command, as if you had typed it and pressed Enter.
The word after <<< is a single item. If it contains spaces and is not quoted, only the first word becomes the input and the rest become ordinary arguments:
cat <<< hello world
Output:
hello
cat: world: No such file or directory
The string hello went to standard input, but world was passed to cat as a file name. Quote the string to keep it together.
The Newline at the End
A here string always adds a newline to the end of the text. You can see the extra character by comparing it with printf, which adds nothing:
od -c <<< "hi"
printf 'hi' | od -c
Output:
0000000 h i \n
0000003
0000000 h i
0000002
The here string delivered three bytes, h, i and a newline, while printf delivered only two. Most text tools expect lines to end with a newline, so the extra byte is usually helpful. It does matter for commands that count characters. An empty variable still produces one empty line:
empty=""
wc -l <<< "$empty"
wc -c <<< "$empty"
Output:
1
1
Expansion in a Here String
The shell expands the string before the command sees it. Variables, command substitutions and arithmetic expansions work, and quotes control them in the usual way:
name="Ana Maria"
wc -w <<< "$name"
grep -o 'Maria' <<< "$name"
Output:
2
Maria
Single quotes switch expansion off, and double quotes allow it:
cat <<< 'single $no expansion'
cat <<< "double $name expansion"
Output:
single $no expansion
double Ana Maria expansion
A command substitution works the same way. This counts the characters in the current year, plus the newline that the here string adds:
wc -c <<< "$(date +%Y)"
Output:
5
A year has four digits. The fifth character is the newline.
Word Splitting Does Not Apply
One difference from command arguments is worth knowing: the shell does not split an unquoted variable in a here string into separate words. The spacing is preserved:
v="a b c"
cat <<< $v
cat <<< "$v"
Output:
a b c
a b c
Both forms printed the same text. Even so, quoting is the clearer habit, because it makes the intention obvious and keeps the line safe if you later move it into an argument list.
Reading Values into Variables
The most common use of a here string is with read. The read command splits a line into fields and stores them in variables:
read -r first second rest <<< "one two three four"
echo "first=$first second=$second rest=$rest"
Output:
first=one second=two rest=three four
The last variable receives everything that is left, which is why rest holds two words. Now compare that with the pipe form from lesson 61:
read -r a b <<< "x y"
echo "a=$a b=$b"
echo "x y" | read -r c d
echo "c='${c:-}' d='${d:-}'"
Output:
a=x b=y
c='' d=''
With a here string, read runs in the current shell, so its variables stay available. With a pipe, read runs in a subshell, and the assignments are lost. This is the main reason to prefer <<< over echo ... | when the receiving command needs to change your variables.
The variable IFS, the internal field separator, chooses the characters that separate fields. Setting it only for the read command splits on that character:
IFS=: read -r user pw uid <<< "root:x:0:0"
echo "user=$user pw=$pw uid=$uid"
Output:
user=root pw=x uid=0:0
The third variable uid received the whole remainder, 0:0, because only three variables were supplied. The IFS=: assignment applies to that single read command and does not change the shell's IFS afterwards.
A date can be taken apart in the same way:
IFS=- read -r y m d <<< "2026-11-21"
echo "y=$y m=$m d=$d"
Output:
y=2026 m=11 d=21
Splitting a String into an Array
The read command can fill an array with its -a option, which is useful for comma-separated values:
IFS=, read -r -a parts <<< "red,green,blue"
echo "count=${#parts[@]} second=${parts[1]}"
Output:
count=3 second=green
The array parts has three elements numbered from 0, so the second element, at index 1, is green. Lessons 97 to 105 cover arrays in detail.
Leading and Trailing Spaces
By default, read removes leading and trailing whitespace from the line, because those characters are part of IFS. Emptying IFS for the command keeps them:
read -r t <<< " spaced out "
printf '[%s]\n' "$t"
IFS= read -r t <<< " spaced out "
printf '[%s]\n' "$t"
Output:
[spaced out]
[ spaced out ]
When reading lines in loops, the form IFS= read -r line is the standard way to keep each line exactly as it is. A later lesson on reading files line by line (114) builds on it.
Looping Over the Lines of a Variable
A while read loop can read from a here string. Because the loop is not part of a pipeline, its variables survive:
list=$'alpha\nbeta\ngamma'
n=0
while IFS= read -r item; do n=$((n+1)); echo "$n: $item"; done <<< "$list"
echo "n after loop: $n"
Output:
1: alpha
2: beta
3: gamma
n after loop: 3
The $'...' form creates a string with real newline characters. The here string passed all three lines to the loop on standard input, and after the loop the counter n still held 3.
Feeding Text to Common Commands
Here strings work with any command that reads standard input. These examples show a few typical uses:
data=$'b\na\nc'
sort <<< "$data"
a
b
c
text="error: disk full"
if grep -q 'disk' <<< "$text"; then echo "match"; fi
match
bc <<< "2 + 3 * 4"
bc <<< "scale=2; 10 / 3"
14
3.33
cut -d' ' -f2 <<< "one two three"
awk '{print $2}' <<< "alpha beta gamma"
sed 's/cat/dog/' <<< "the cat sat"
two
beta
the dog sat
The calculator bc is not always installed on minimal systems, so check for it before depending on it in a script.
The same technique handles a whole line of words. This converts a space-separated string into one word per line and removes duplicates:
tr ' ' '\n' <<< "b a b c a" | sort -u
a
b
c
A here string can also sit inside a test, as in this check on the number of words:
if [[ $(wc -w <<< "a b c") -eq 3 ]]; then echo "three words"; fi
three words
Portability
Here strings are not part of the POSIX shell standard. Dash, the shell that sh points to on Debian and Ubuntu, rejects them with a syntax error:
dash -c 'cat <<< "hi"'
Output:
dash: 1: Syntax error: redirection unexpected
In a script that must run under sh, use a pipe, printf '%s\n' "$text" | command, or a here document. In scripts that start with #!/usr/bin/env bash, here strings are safe to use.
The next lesson, on command lists, shows how to combine commands with ;, && and ||.