Bash: Syntax error: redirection unexpected

If you're using the following to run your script:

sudo sh ./script.sh

Then you'll want to use the following instead:

sudo bash ./script.sh

The reason for this is that Bash is not the default shell for Ubuntu. So, if you use "sh" then it will just use the default shell; which is actually Dash. This will happen regardless if you have #!/bin/bash at the top of your script. As a result, you will need to explicitly specify to use bash as shown above, and your script should run at expected.

Dash doesn't support redirects the same as Bash.


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!

Tags:

Bash

Ubuntu