bashubuntu

Bash: Syntax error: redirection unexpected


I do this in a script:

read direc <<< $(basename `pwd`)

and I get:

Syntax error: redirection unexpected

in an ubuntu machine

/bin/bash --version
GNU bash, version 4.0.33(1)-release (x86_64-pc-linux-gnu)

while I do not get this error in another suse machine:

/bin/bash --version
GNU bash, version 3.2.39(1)-release (x86_64-suse-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.

Why the error?


Solution

  • Does your script reference /bin/bash or /bin/sh in its hash bang line? The default system shell in Ubuntu is dash, not bash, so if you have #!/bin/sh then your script will be using a different shell than you expect. Dash does not have the <<< redirection operator.

    Make sure the shebang line is:

    #!/bin/bash
    

    or

    #!/usr/bin/env bash
    

    And run the script with:

    $ ./script.sh
    

    Do not run it with an explicit sh as that will ignore the shebang:

    $ sh ./script.sh   # Don't do this!