Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reusing output from last command in Bash

Is the output of a Bash command stored in any register? E.g. something similar to $? capturing the output instead of the exit status.

I could assign the output to a variable with:

output=$(command)

but that's more typing...

like image 200
memecs Avatar asked Jun 18 '14 10:06

memecs


People also ask

How do you repeat the last command in bash?

To get the same effect as using the up arrow once, which is showing the last command run without executing it, you can use CTRL+P. Or change the number, and run any of your last commands. For example changing the 1 to a 2 will run the second from last command. This is all made possible by using bash history.

How do you Undo the last command in bash?

In Bash and Zsh ctrl + w erases backwards from where the cursor is.

What is the output of last command?

The last command in Linux is used to display the list of all the users logged in and out since the file /var/log/wtmp was created. One or more usernames can be given as an argument to display their login in (and out) time and their host-name.


2 Answers

You can use $(!!) to recompute (not re-use) the output of the last command.

The !! on its own executes the last command.

$ echo pierre
pierre
$ echo my name is $(!!)
echo my name is $(echo pierre)
my name is pierre
like image 187
ling Avatar answered Oct 14 '22 00:10

ling


The answer is no. Bash doesn't allocate any output to any parameter or any block on its memory. Also, you are only allowed to access Bash by its allowed interface operations. Bash's private data is not accessible unless you hack it.

like image 116
konsolebox Avatar answered Oct 14 '22 01:10

konsolebox