Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

What does <<< mean? Here is an example:

$ sed 's/a/b/g' <<< "aaa"
bbb

Is it something general that works with more Linux commands?

It looks like it's feeding the sed program with the string aaa, but isn't << or < usually used for that?

share|improve this question

marked as duplicate by don_crissti, Mat, Anthon, Michael Mrozek Jun 22 '13 at 20:47

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4 Answers 4

up vote 8 down vote accepted

Others have answered the basic question: what is it?

Now, why is it useful? You can also feed a string to a command's stdin like this:

echo "$string" | command

However in bash, introducing a pipe means the individual commands are run in subshells. Consider this:

echo "hello world" | read first second
echo $second $first

The output of the 2nd echo command a single space. Whaaaa? What happened to my variables? Because the read command is in a pipeline, it is run in a subshell. It correctly reads 2 words from its stdin and assigns to the variables. But then the command completes, the subshell exits and the variables are lost.

Sometimes you can work around this with braces:

echo "hello world" | {
    read first second
    echo $second $first
}

That's OK if your need for the values is contained, but you still don't have those variables in the current shell of your script. To remedy this confusing situation, use a here-string

read first second <<< "hello world"
echo $second $first

Ah, much better!

share|improve this answer
1  
In addition to here-strings, process substutitions are very useful for the same reasons. –  glenn jackman Jun 22 '13 at 18:32
1  
+1 for going the extra mile. –  Joseph R. Jun 22 '13 at 18:37

Take a look at the Bash man page. This notation is part of what's called a here documents & here strings. It allows you the ability to generate multi-line data input as one continuous string. The variation you're asking about is called a here string.

excerpt from Bash man page

Here Strings
   A variant of here documents, the format is:

          <<<word

   The word is expanded and supplied to the command on its standard input.
share|improve this answer

<<< denotes a here string.

$ cat <<< 'hi there'
hi there

It passes the word on the right the the standard input of the command on the left.


<< denotes a here document.

$ cat <<EOF
> hi
> there
> EOF
hi
there

EOF can be any word.

Here documents are commonly used in shell scripts to create whole files or to display long messages.

cat > some-file <<FILE
foo
bar
bar bar
foo foo
FILE

< passes the contents of a file to a command's standard input.

$ cat < /etc/fstab
/dev/sda2               /boot   ext4            nosuid,noexec,nodev,rw,noatime,nodiratime       0 2
/dev/sda4               /       ext4            rw,noatime,nodiratime,  0 1
/dev/sdb5               /var    ext4            nosuid,noexec,nodev,rw,relatime 0 2
 ...
share|improve this answer

It means here strings.

<<< strings

The strings is expanded and supplied to the command on its standard input.

In your example, strings aaa is feed to sed command via stdin.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.