0

I have this:

timeout 25 bash -c '
  for i in {1..9}; do
      if read line < "$my_fifo"; then
          if test "$line" != "0"; then
                   exit 0;
           fi
      fi
  done
'

I don't really like that bash can't do this:

timeout 25 (...)

I don't understand why () is not considered a program by itself. Just an anonymous program... anyway..

my goals is to achieve the above but without the need to use bash -c 'xyz' since I won't get syntax highlighting in the quotes etc.

Is there a workaround for this?

2
  • bash can't do this I don't think it has anything to do with bash or whatever shell you use. Rather it's because the program (timeout), which is not a shell, expects/executes another program. A program here refers to an executable, and (...) is a part of the shell script itself. Bash can't make it an executable for timeout to "chainload" it. Commented Nov 22, 2023 at 4:15
  • 1
    Note that bash's read builtin supports a -t option as well. Commented Nov 22, 2023 at 6:08

1 Answer 1

2

A workaround in Bash may be to define a function, export it, finally use timeout 25 bash -c to run the function. This is even less "directly", but at least syntax highlighting should work.

Frankly, since timeout is a separate program, I think there is no way to execute "directly". timeout 25 (...) would be possible if Bash implemented its own timeout and made it a keyword (like time; time (...) works).

Note in the below example we put $my_fifo in the environment of timeout. Your original code also requires $my_fifo in the environment.

#!/bin/bash
my_func() {
  for i in {1..9}; do
      if read line < "$my_fifo"; then
          if test "$line" != "0"; then
                   exit 0;
           fi
      fi
  done
}
export -f my_func
my_fifo=./fifo timeout 25 bash -c my_func
1
  • Note that if you add set -o allexport at the start of the script, all variables and functions defined from then on are exported to the environment so would be available to the separate bash invocation. Commented Nov 22, 2023 at 6:07

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.