我如何从c程序/shell脚本中触发历史命令



我尝试从C编程,但给出错误"sh: 1:历史记录:not found"。
我的c编程代码

int main ()
{
   char command[50];
   strcpy( command,"history | tail -20" );
   system(command);
   return(0);
}

bash中您可以访问环境变量$HISTFILE。使用参数-ci调用shell。-c用于运行命令,-i用于为shell提供一个具有所有可用环境变量的交互式环境。

bash -ic 'tail -20 $HISTFILE'

最新更新