Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using output of previous commands in bash

Tags:

linux

bash

shell

In Mathematica, it is possible to reuse the output of the previous command by using %.

Is something similar possible for bash (or some other shell)?

For example, I run a make which gives warnings, but I want to find all warnings. So, I type

make | grep "warning"

but I'm not able to see the output of the make then.

I would like to type something like this instead:

make
% | grep "warning"
like image 546
Harsh Avatar asked Jul 21 '10 09:07

Harsh


People also ask

What in bash lets you take the output of a previous command and use it as input in the next command?

You can make it do so by using the pipe character '|'. Pipe is used to combine two or more commands, and in this, the output of one command acts as input to another command, and this command's output may act as input to the next command and so on.

How do I find previous commands in bash?

You can search command from the history by pressing Ctrl+R. When these keys are pressed then a search option will appear. The command will search from the history based on the keypress by the user.

How do you view the previous commands which you have already executed?

(reverse-i-search)`': After you have typed what you are looking for, use the CTRL-R key combination to scroll backward through the history. Use CTRL-R repeatedly to find every reference to the string you've entered. Once you've found the command you're looking for, use [Enter] to execute it.

What is the output of last command?

The last command displays information about the last logged-in users. It's pretty convenient and handy when we need to track login activities or investigate a possible security breach. The last command will, by default, take the system log file /var/log/wtmp as the data source to generate reports.


2 Answers

Since the amount of output is indeterminate, it doesn't make sense for bash to store it for you for re-display. But there's an alternate solution to your problem:

The tee command allows you to duplicate an output stream to a file. So if you're willing to use a file for temporary storage, you can do something like this:

make | tee output.txt
grep "warning" output.txt

This solution avoids running make twice, which could be (a) expensive and (b) inconsistent: the second make may be doing less work than the first because some targets were already made the first time around.

Note: I haven't tried this. You may need to fiddle with joining the error and output streams, or such.

like image 120
Carl Smotricz Avatar answered Oct 12 '22 11:10

Carl Smotricz


You could do this:

make
!! | grep "warning"

By using !! you tell it to repeat the last command in that spot, along with any other bash commands you want to add to it.

The downside is that if the command you are repeating takes a long time to execute, you'll have that much longer to wait unless you stored the output of the previous command to an output file first.

like image 37
Ryan Hart Avatar answered Oct 12 '22 12:10

Ryan Hart