0

Running the following script as myself works but not when I use sudo. As you can probably tell, the touch error is just to make sure something is written to the log file.

dave@pi1:~ $ ./test.sh
touch: cannot touch '/asdf/asdf/asdf': No such file or directory  <--- As expected
dave@pi1:~ $ sudo ./test.sh
./test.sh: 2: ./test.sh: Syntax error: redirection unexpected  <--- why this not work?
dave@pi1:~ $

The script:

LOG_FILE=/var/log/usbhook
exec > >(tee -a ${LOG_FILE} )
exec 2> >(tee -a ${LOG_FILE} >&2)

touch /asdf/asdf/asdf

The aim is to have a script fired when I plug in a USB stick so I'm assuming it should work when ran using sudo.

9
  • 4
    Add #!/usr/bin/env bash as first line in your script Commented Aug 16, 2019 at 14:58
  • 1
    Also see How to use Shellcheck. Commented Aug 16, 2019 at 15:16
  • 1
    @anubhava thanks, that did the trick. Commented Aug 19, 2019 at 8:25
  • 1
    @dank8, $0 is not reliably the shell name. It can be the script name, it can be a completely arbitrary string passed at the beginning of argv, etc. Commented May 27, 2023 at 13:57
  • 1
    And this is like the duplicate: >( is a syntax element that can't be used in sh. Just because one operator is >( ) and the other is <<< doesn't make it two different questions; it's the same misunderstanding about what it takes to ensure that bash-only syntax is available. Commented May 27, 2023 at 13:59

1 Answer 1

1

Bash: Syntax error: redirection unexpected

^Similar, and might answer your question. From the accepted answer in that thread:

"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."

Sign up to request clarification or add additional context in comments.

3 Comments

I don't believe that's the same. That's about a particular operator which isn't supported on a different Linux flavour
// , I agree with Ted that although the solutions might be the same (add a shebang, noob!) the questions could be different. Ted, what's different about your question?
We don't care if the questions are different, actually. This is a duplicate because the answer is the same. We would have mountains of duplicates if we would allow every minor variation of a basic problem to be a new question. This is bad for the site because then nobody can find the right one in the haystack of very similar questions.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.