Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

system("history") not working

Tags:

c++

linux

I've run into a snag, I'm trying to implement a linux shell program of sorts with C++ and many of my commands seem to work, however, when I try to get the history(list all recently executed commands) I get an error of "sh: 1: history: not found" the below line is all that runs in the area, what is the issue?

 system("history"); //produces the error above ^

If I do

$ history

from the command line it's fine...why is it not fine in C++?

like image 577
snurby77 Avatar asked Nov 27 '25 05:11

snurby77


2 Answers

system executes a program using /bin/sh, but history is a bash builtin.

You might look at the contents of ~/.bash_history instead. (Note (by leemes) .bash_history is only updated after closing a previous bash session, as well as it is not updated by executing a command with system.)

like image 154
Csq Avatar answered Nov 29 '25 17:11

Csq


Because it's a bash shell builtin not necessarily accessible through /bin/sh -c` (which may be the bourne shell).

like image 29
Paul Evans Avatar answered Nov 29 '25 19:11

Paul Evans



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!