23

I'm getting this error

Syntax error: redirection unexpected

in the line:

 if grep -q "^127.0.0." <<< "$RESULT"

How I can run this in Ubuntu?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
jmginer
  • 361
  • 1
  • 3
  • 7

3 Answers3

35

<<< is a bash-specific redirection operator (so it's not specific to Ubuntu). The documentation refers to it as a "Here String", a variant of the "Here Document".

3.6.7 Here Strings

A variant of here documents, the format is:

<<< word

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

A simple example:

$ cat <<< hello
hello

If you're getting an error, it's likely that you're executing the command using a shell other than bash. If you have #!/bin/sh at the top of your script, try changing it to #!/bin/bash.

If you try to use it with /bin/sh, it probably assumes the << refers to a "here document", and then sees an unexpected < after that, resulting in the "Syntax error: redirection unexpected" message that you're seeing.

zsh and ksh also support the <<< syntax.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • ksh supports it. dash doesn’t. – Josh Lee Apr 16 '13 at 19:37
  • Would it be accurate to say it's a shorthand for `cat <(echo "hello")`? (I'm just asking to deepen my understanding) – Sridhar Sarnobat Oct 17 '17 at 19:26
  • 7
    @Sridhar-Sarnobat: Not quite. `cat <<< hello` invokes `cat` with no arguments, with stdin redirected from a temporary file containing `hello` followed by a newline. `cat <(echo hello)` invokes `cat` with a single argument, which is the name of a pseudo-file from which it can read the same data. `cat` behaves similarly with no arguments and stdin redirected vs. with a single argument naming a file to be read, but other commands can behave differently. For example, on my system `ls -l /proc/self/fd/0 << – Keith Thompson Oct 17 '17 at 22:59
25
if grep -q "^127.0.0." <<< "$RESULT"
then
    echo IF-THEN
fi

is a Bash-specific thing. If you are using a different bourne-compatable shell, try:

if echo "$RESULT" | grep -q "^127.0.0."
then
    echo IF-THEN
fi
Hal Canary
  • 2,154
  • 17
  • 17
2

It works for me on Ubuntu, if I complete you IF block:

if grep -q "^127.0.0." <<< "$RESULT"; then echo ""; fi
Jakuje
  • 24,773
  • 12
  • 69
  • 75