Why is the command md5sum <<< 'ddd'
(output: d6d88f2e50080b9602da53dac1102762 -
)
right, and md5sum << 'ddd'
not?
What does <<<
mean?
-
What's the difference between <<, <<< and < < in bash?Nick Dong– Nick Dong2023年02月21日 02:42:32 +00:00Commented Feb 21, 2023 at 2:42
2 Answers 2
The <<<
starts a "here string": The string is expanded and fed to the program’s stdin. (In your case, there is not much of expansion happening.) It is equivalent to this:
echo ddd | md5sum
On the other hand, <<
starts a here document. All the following lines up to one containing the marker ddd
will comprise the input of the program. (You should use a marker that is not likely to appear in your data.) You could achieve the same effect as above like this:
md5sum <<END
ddd
END
There is one difference between <<END
and <<'END'
: Without the quotes, any variables, escape sequences etc. in the here document will be expanded as usual.
-
6Where it's not equivalent is that except in
rc
,<<<
(like<<
) uses a temporary file so can be used by commands thatlseek
their stdin.Stéphane Chazelas– Stéphane Chazelas2013年05月19日 22:05:20 +00:00Commented May 19, 2013 at 22:05 -
@Stéphane Chazelas, where this temporary is created? I've tried to start using
<<
and in other window typedls -la
in same folder and have not seen any new files (Mac OS).Alex Martian– Alex Martian2020年01月10日 06:39:13 +00:00Commented Jan 10, 2020 at 6:39 -
2@AlexeiMartianov, it's deleted after it's opened but before it's being used which ensures it's not left behind, so it's only visible on the file system for a very short time. On Linux
ls -ld /proc/self/fd/0 <<< text
will show you its original path with a" (deleted)"
appended. It's generally in$TMPDIR
or/tmp
if$TMPDIR
is not set.zsh
uses$TMPPREFIX
instead (a prefix, not a directory).Stéphane Chazelas– Stéphane Chazelas2020年01月10日 06:54:32 +00:00Commented Jan 10, 2020 at 6:54 -
So would it be correct to say that a here-string
<<<
doesn't need closing, and is typically all on a single line?Sridhar Sarnobat– Sridhar Sarnobat2022年05月12日 07:12:34 +00:00Commented May 12, 2022 at 7:12
<<<
introduces a here string: the string after <<<
is passed as input to the command. This originates in Byron Rakitzis's implementation of rc
(a Plan 9 shell) for Unix, and is also present in zsh, ksh93, mksh, yash and bash.
<<
introduces a here document: subsequent lines of the shell script are passed as input to the command, and the string after <<
is a terminator. Here documents work in all Bourne-style shells (Bourne, POSIX, ash, bash, ksh, zsh, ...), C-style shells (csh, tcsh), and shells derived from the Plan 9 shell (rc, es, akanga).
-
5No,
<<<
is not aksh
extension, the path isrc
->zsh
->ksh93
->bash
(ksh
release notes acknowledge for once borrowing the feature fromzsh
).<<
also works inrc
style shellsStéphane Chazelas– Stéphane Chazelas2013年05月19日 21:52:19 +00:00Commented May 19, 2013 at 21:52 -
1There's a difference between the
rc
andzsh
<<<
though in thatrc
's doesn't include a trailing newline character and doesn't use a temp file (uses a pipe and an extra process feeding it at least in the port to Linux).Stéphane Chazelas– Stéphane Chazelas2013年05月19日 22:02:24 +00:00Commented May 19, 2013 at 22:02