bash - 无法在 .sh 脚本中回显上次执行
问题描述
使用以下
cmd=$(uname -a)
printf "["!:0"]"
给我
[cmd1=Linux localhost.localdomain 2.6.32-300.10.1.el5uek #1 SMP Wed Feb 22 17:37:40 EST 2012 x86_64 x86_64 x86_64 GNU/Linux]
但是什么时候完成test.sh
#!/bin/bash
output=$(uname -a)
printf "["!:0"]"
我越来越
[!:0]
谢谢。
解决方案
如果您需要在 bash ( v4+
) 脚本中执行上一个命令,
#!/bin/bash -i
# -i: interactive mode, enable history inside script
set -o history # enable history, for old bash versions
cmd=$(uname -a)
history | sed -n 'x;${s/ *[0-9]\+ *\(\S*\)/\1/p}' # print the line before last history entry after removing index
推荐阅读
- java - 切换场景保持状态 JavaFx
- react-native - 如何使用 createBottomTabNavigator 中的导航按钮
- android - 什么时候在android webview中调用shouldInterceptRequest()
- python - 为 select_related 添加注解值
- python - 使用 matplotlib 绘制日期有间隙的数据框
- excel - 在特定日期期间,根据过去的月份生成新行
- php - 统计网站的 Laravel 控制器配置
- mongodb - 为什么 docker-compose 不会为 mongo 打开端口?
- javascript - 如何使用具有原始实现作为依赖项的实现来模拟(开玩笑)
- apache-spark - 尽管控制台接收器工作正常,但写入内存接收器时出现火花流问题