将最后一个命令存储在Bash中的变量中 [英] Store last command in a variable in Bash

查看:40
本文介绍了将最后一个命令存储在Bash中的变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将最新命令存储在变量中.我尝试了 !!:p 历史记录扩展,它确实为我提供了最后一条命令,但无法将其存储在变量中.

I'm want to store the most recent command in a variable. I tried the !!:p history expansion, it does get me the last command but I can't store it in a variable.

$ last=`!!:p`
last=`ls`
$ echo $last

$

有帮助吗?

推荐答案

fc 命令可用于检索先前的命令.

The fc command can be used to retrieve the previous command.

some_var=$(fc -nl -1)

这篇关于将最后一个命令存储在Bash中的变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆