history命令在脚本里面执行不成功解决方案

博客介绍了Linux中bash内置命令与普通命令的区别,指出which查不到的是bash内置命令。重点提及history是bash内置命令,其开启由bash决定,可通过set -o查看是否开启,还介绍了交互式查看、脚本里查看及优化脚本等相关内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

linux bash内置命令与普通命令的区别:which查不到的都是bash内置命令

[root@localhost ~]# which cd
/usr/bin/cd
[root@localhost ~]# which history
/usr/bin/which: no history in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)

history是bash内置命令,能不能开启由bash说了算

set -o查看是否开启

交互式查看

[root@localhost ~]# set -o | grep history
history        	on

在脚本里面查看

[root@localhost ~]# echo "set -o | grep history" > a.sh
[root@localhost ~]# sh a.sh
history        	off

优化脚本

[root@localhost ~]# echo '#!/bin/bash' > a.sh
[root@localhost ~]# echo 'HISTFILE=~/.bash_history' >> a.sh
[root@localhost ~]# echo "set -o history" >> a.sh          # 打开bash的history配置  
[root@localhost ~]# echo "set -o | grep history" >> a.sh
[root@localhost ~]# sh a.sh
history        	on
[root@localhost ~]# echo 'history | tail' >> a.sh
[root@localhost ~]# sh a.sh
history        	on
   59  exit
   60  ifconfig
   61  ss -nutpl|grep ssh
   62  cat /etc/hosts | tew /tmp/hostsfile && cat /tmp/hostsfile
   63  cat /etc/hosts | tee /tmp/hostsfile && cat /tmp/hostsfile
   64  cat /etc/hosts | tee /tmp/hostsfile
   65  cat /tmp/hostsfile
   66  poweroff
   67  set -o | grep history
   68  history | tail
[root@localhost ~]# cat a.sh
#!/bin/bash
HISTFILE=~/.bash_history
set -o history
set -o | grep history
history | tail

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值