dash and build-in time command: not expected behavior

Colin Watson cjwatson at ubuntu.com
Wed Jun 18 20:23:18 UTC 2014

On Wed, Jun 18, 2014 at 05:55:03PM +0200, Stefan Schweter wrote:
> I'm currently found out some "strange" behavior with the built-in
> time command in combination with dash:
> $ time echo "test" | wc -l | sleep 5
> 0.00user 0.00system 0:00.00elapsed ?%CPU (0avgtext+0avgdata
> 620maxresident)k
> 0inputs+0outputs (0major+200minor)pagefaults 0swaps
> I expected that the time command measure the time for the whole pipe
> construct - but in this case I guess it finishes after the echo
> command.
> Same command in a bash returns:
> time echo "test" | wc -l | sleep 5

bash is the one that's strange here, not dash.  No other command can
possibly behave this way - it actually requires "time" to be a reserved
word and have special treatment in the shell grammar!  This is specific
to (I think) ksh and its descendants, including bash, and you can't make
dash do it directly.

However, you can do this instead, which is portable to any POSIX shell:

  time sh -c 'echo "test" | wc -l | sleep 5'

Colin Watson                                       [cjwatson at ubuntu.com]

More information about the ubuntu-users mailing list