linux:将最后一个命令存入变量

Han*_*ung 3 linux bash shell

我准备将最后一个命令存储到变量中.

例如:

#last command:
$> cp myfile /to/somewhere
$> last = #some magical command here
$> echo $last 
cp myfile /to/somewhere
Run Code Online (Sandbox Code Playgroud)

我在!!:p命令上尝试了几种方式,它确实让我获得了最后一个命令,但我无法将其存储到变量中.

$> last=`!!:p`
$> echo $last
(empty)
Run Code Online (Sandbox Code Playgroud)

有帮助吗?

cod*_*min 8

尝试some_var=$(fc -nl -1),fc命令可用于检索以前的命令

从评论中加入了更好的例子.