1

I just realized that history does not output anything in a subshell. I'll try to show by counting lines - first a sanity check with echo:

$ echo a | wc -l
1

... and in a subshell:

bash -c 'echo a' | wc -l
1

Indeed, we expect one line output in both cases. Now, for history:

$ history | wc -l
681

... but in a subshell:

$ bash -c 'history' | wc -l
0

... history returns no lines.

Why does this happen - and how can I get the history in the subshell to output the history of the current shell (including last session commands)?

sdbbs
  • 480

1 Answers1

7

-c doesn't create a subshell; it just creates a non-interactive new shell (or rather, a non-interactive execution environment in a separate process that has nothing to do with your current shell). And: in non-interactive shells, the history command is intentionally non-functional:

History command inside bash script