Why is my bash "history" command 56x slower in a subshell?

182 views Asked by At

Why does running the bash history builtin function in a subshell run 56x slower?

(I have 22k lines in my history. Maybe that is significant?)

$ time history > /dev/null

real    0m0.064s
user    0m0.049s
sys     0m0.015s

$ # in a subshell it runs 56x slower:
$ time (history > /dev/null)

real    0m4.780s
user    0m2.558s
sys     0m2.215s

$ # trying "date" instead to show "time" isn't the issue:

$ date; history > /dev/null; date
Wed Oct 21 21:50:51 PDT 2020
Wed Oct 21 21:50:51 PDT 2020  (< 1 sec)

$ date; (history > /dev/null); date
Wed Oct 21 21:50:51 PDT 2020
Wed Oct 21 21:50:56 PDT 2020  (5 secs)

$ # each command in a pipeline is executed in a subshell (bash(1))
$ date; history | tail -1 > /dev/null; date
Wed Oct 21 21:50:56 PDT 2020
Wed Oct 21 21:51:01 PDT 2020  (5 secs)

$ history|wc -l
   22267

$ # time for a simple command ("date")
$ time date
Sat Jul  2 17:40:33 PDT 2022

real    0m0.004s
user    0m0.001s
sys     0m0.003s

$ bash --version
GNU bash, version 4.4.12(1)-release (x86_64-apple-darwin15.6.0)

Mac OS version: macOS Mojave 10.14.6

0

There are 0 answers