Automatically Capture Output of Last Command into a Variable Using Bash

Automatically capture output of last command into a variable using Bash?

This is a really hacky solution, but it seems to mostly work some of the time. During testing, I noted it sometimes didn't work very well when getting a ^C on the command line, though I did tweak it a bit to behave a bit better.

This hack is an interactive mode hack only, and I am pretty confident that I would not recommend it to anyone. Background commands are likely to cause even less defined behavior than normal. The other answers are a better way of programmatically getting at results.


That being said, here is the "solution":

PROMPT_COMMAND='LAST="`cat /tmp/x`"; exec >/dev/tty; exec > >(tee /tmp/x)'

Set this bash environmental variable and issues commands as desired. $LAST will usually have the output you are looking for:

startide seth> fortune
Courtship to marriage, as a very witty prologue to a very dull play.
-- William Congreve
startide seth> echo "$LAST"
Courtship to marriage, as a very witty prologue to a very dull play.
-- William Congreve

Capture output of last executed command into a variable without affecting Vim and line returns

I think what you ask for is hard do achieve. Vim tests if the output is a terminal. The command you've provided redirects the output to the tee command. tee saves its input (which also menans: command's output) to the file and outputs it to the terminal. But vim knows nothing about it. It only knows its output is not a terminal. So it outputs warning. And from the vim's source code:

[...]
if (scriptin[0] == NULL)
ui_delay(2000L, TRUE);
TIME_MSG("Warning delay");

which means this redirection will always get you 2 seconds delay.

Also, for example, man vim command will not work with such redirections, because terminal output has some attributest (e.g. width and height) which generic file hasn't. So... it won't work.

Capture command output into a local variable and check for error

Exit code of local password=$(...) is the status of the local command (which should ideally be 0). local is treated as a command and the return code of $(...) is overridden by that.

To avoid that, create the local variable first and then do the assignment.

i.e.

local password    # return code of this is always 0
password=`whiptail --clear \
--title "Password" \
--passwordbox "Enter your login password:" \
8 68 \
2>&1 >/dev/tty`
local err=$?

Get the last echo statement inside a function and put inside variable in bash

Use tail to extract just the last line.

result=$(statement | tail -n 1)

copy text from the output of a previous command (using only keyboard, not mouse)

Once something is printed, it is gone, and bash does not know anything about it anymore. bash can intercept your keystrokes, but does not have stored anywhere the previous output. The best you could do is to use tee:

./my_lovely_program input.txt | tee some_file

where some_file would contain the standard output of your program.



Related Topics



Leave a reply



Submit