Since it’s mentioned in the article itself, the way time works is also somewhat counter-intuitive - it modifies the entire pipeline rather than an individual command - but makes sense if you think about it: when running time echo | echo you probably want to know how much time the entire pipeline takes, rather than the first command.
What this also means that bash’s time doesn’t support echo | time echo. However, if you do something like echo | time echo, it probably will work, because time is also frequently available as an external command.
I hope it’s not too off-topic to plug Elvish, which doesn’t have the obscure ! stuff, and where time works in the most logical way: it’s a builtin command rather than part of the syntax, and it takes a lambda. You can write time { echo | echo } or echo | time { echo } and they both do exactly what you would expect. (And if you want to benchmark some code multiple times, just use benchmark instead.)
In fact time is my go-to example for why shells actually should have lambdas, and not having them ultimately leads to some very weird and surprising semantics!
The questions assume that you know what ! syntax does in Bash, and that the only background you need is where that symbol can be used according to the manual. But I have no idea what ! is supposed to do, so it feels pointless to guess where it could be written.
It’s usually used as part of if ! whatever; then ...; fi where it seems obvious what it does if you know that true is command success (0 exit code) and false is non-zero exit codes. The shell being what it is, if conditions are just commands and can be run on their own as shown in the quiz.
Ah, I’m glad I could have my morning reminder that shells are really great for starting programs written in sane programming languages.
Another of those reasons that I hate seeing any bash scripts that do such magic
Since it’s mentioned in the article itself, the way
timeworks is also somewhat counter-intuitive - it modifies the entire pipeline rather than an individual command - but makes sense if you think about it: when runningtime echo | echoyou probably want to know how much time the entire pipeline takes, rather than the first command.What this also means that bash’s
timedoesn’t supportecho | time echo. However, if you do something likeecho | time echo, it probably will work, becausetimeis also frequently available as an external command.I hope it’s not too off-topic to plug Elvish, which doesn’t have the obscure
!stuff, and wheretimeworks in the most logical way: it’s a builtin command rather than part of the syntax, and it takes a lambda. You can writetime { echo | echo }orecho | time { echo }and they both do exactly what you would expect. (And if you want to benchmark some code multiple times, just usebenchmarkinstead.)In fact
timeis my go-to example for why shells actually should have lambdas, and not having them ultimately leads to some very weird and surprising semantics!The questions assume that you know what
!syntax does in Bash, and that the only background you need is where that symbol can be used according to the manual. But I have no idea what!is supposed to do, so it feels pointless to guess where it could be written.It’s usually used as part of
if ! whatever; then ...; fiwhere it seems obvious what it does if you know that true is command success (0 exit code) and false is non-zero exit codes. The shell being what it is,ifconditions are just commands and can be run on their own as shown in the quiz.