2

Using Cygwin (W10) I've historically used this to measure the execution time within a script:

#!/bin/sh

(time {
sleep 1;
})

I've now moved to Debian (Windows Subsystem for Linux) and the same script gives this error:

Syntax error: "}" unexpected (expecting ")")



I also tried the two suggested scripts here: https://askubuntu.com/a/431184 but both fail:

$ foo() {
    sleep 1;
    echo "Hello World"
  }
$ time foo
Hello World 

and

$ foo() {
    sleep 1;
    echo "Hello World"
  }
$ export -f foo
$ echo foo | /usr/bin/time /bin/bash
Hello World

with the same error message:

Syntax error: "(" unexpected


By the way, I've installed /usr/bin/time.

Could someone please help?

Thank you

EDIT: as requested, the script is simply:

#!/bin/bash

(time {
sleep 1;
})
NoExpert
  • 469
  • 4
  • 14
  • Possible duplicate of [Does the shebang determine the shell which runs the script?](https://unix.stackexchange.com/questions/87560/does-the-shebang-determine-the-shell-which-runs-the-script) – muru Oct 19 '19 at 12:57

1 Answers1

5

/bin/sh on Cygwin is likely a symlink to bash, on Debian it's dash. On bash, time is a keyword that can act on shell command lines. Dash doesn't have an equivalent, and there time is just the external /usr/bin/time, and that doesn't act at the same level as the time keyword, so it can't be used on command groups.

Use /bin/bash for your shebang if you're going to use bashisms like the time keyword.

muru
  • 69,900
  • 13
  • 192
  • 292
  • Thanks for replying. I just tried `#!/bin/bash (time { sleep 1; })` and the result is the same: Syntax error: "}" unexpected (expecting ")") Perhaps it's a more basic syntax error (i.e. I've messed up the brackets)? Thanks – NoExpert Oct 19 '19 at 11:21
  • Please edit your question to show the updated script and how you ran it. – muru Oct 19 '19 at 11:22
  • The updated script works fine enough for me. How are you executing it? – muru Oct 19 '19 at 11:35
  • That's strange. I simply do `sh time.sh` from the Debian WSL window. It then complains that on line 5 `Syntax error: "}" unexpected (expecting ")")` I wonder if this is something specific to WSL. – NoExpert Oct 19 '19 at 11:51
  • Because you're still executing your script with `sh` not bash. Use `bash time.sh` – glenn jackman Oct 21 '19 at 14:26