263

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...

3

16 Answers 16

303

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
9
  • 13
    You can save yourself from typing $(your_cmd) by using backticks: `your_cmd` According to the Gnu Bash manual they are functionally identical. Of course this is also subject to the warning raised by @memecs
    – user2065875
    Apr 20, 2015 at 21:33
  • 1
    In practice, the output of commands is deterministic so I wouldn't agonize over it recomputing. It's certainly useful when you want to do something like git diff $(!!) where the previous command was a find invocation. Apr 4, 2018 at 0:32
  • 2
    Apparently there are good reasons to use $() instead of backticks. But probably nothing to lose sleep over. github.com/koalaman/shellcheck/wiki/SC2006 Jan 2, 2020 at 15:58
  • 3
    @user2065875 although backticks still work in Bash, they are deprecated in favor of $().
    – kurczynski
    Feb 28, 2020 at 1:10
  • 1
    What's the difference between recomputing ($(!!)) and executing (!!)? Dec 8, 2020 at 1:00
124

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.

5
  • 6
    Is it possible to have some custom bash 'middleware' function which runs before every command, where all it does is save the resulting output to something that acts as an internal clipboard (say, BASH_CLIPBOARD)? Dec 31, 2018 at 17:14
  • @PatNeedham Not sure. Check loadable modules. See if you can write one.
    – konsolebox
    Jan 1, 2019 at 13:55
  • 1
    FALSE. The terminal display buffer is where output is allocated and stored. And that’s where it can be read from again. Mar 3 at 13:53
  • @Evi1M4chine Right and the terminal isn't Bash.
    – konsolebox
    Mar 3 at 16:00
  • Is any other shell (zsh etc.) able to do that?
    – Perdu
    Sep 8 at 9:16
81

Very Simple Solution

One that I've used for years.

Script (add to your .bashrc or .bash_profile)

# capture the output of a command so it can be retrieved with ret
cap () { tee /tmp/capture.out; }

# return the output of the most recent command that was captured by cap
ret () { cat /tmp/capture.out; }

Usage

$ find . -name 'filename' | cap
/path/to/filename

$ ret
/path/to/filename

I tend to add | cap to the end of all of my commands. This way when I find I want to do text processing on the output of a slow running command I can always retrieve it with ret.

16
  • 2
    The oneline function definition is wrong. To avoid bash error use cap () { tee /tmp/capture.out; } and ret () { cat /tmp/capture.out; }
    – Enbugger
    Aug 19, 2021 at 11:02
  • 1
    You can also do this with xclip: xci() { xclip -i; xclip -o; }; xco() { xclip -o; };.
    – aparkerlue
    Dec 1, 2021 at 22:43
  • Did not know about xclip! Thank you
    – Connor
    Dec 1, 2021 at 22:44
  • I've tried this wit zsh and it's not working.
    – nyxee
    Mar 1, 2022 at 23:07
  • 1
    @TeddyC that would be really nice, but I don't know a way to do it. If you find a way please let me know
    – Connor
    May 9, 2022 at 16:14
15

If you are on mac, and don't mind storing your output in the clipboard instead of writing to a variable, you can use pbcopy and pbpaste as a workaround.

For example, instead of doing this to find a file and diff its contents with another file:

$ find app -name 'one.php' 
/var/bar/app/one.php

$ diff /var/bar/app/one.php /var/bar/two.php

You could do this:

$ find app -name 'one.php' | pbcopy
$ diff $(pbpaste) /var/bar/two.php

The string /var/bar/app/one.php is in the clipboard when you run the first command.

By the way, pb in pbcopy and pbpaste stand for pasteboard, a synonym for clipboard.

3
  • 2
    on linux $ find app -name 'one.php' | xclip $ diff $(xclip -o) /var/bar/two.php
    – si-mikey
    Mar 13, 2018 at 15:07
  • interesting, I'd never thought of using pbpaste together with command substitution. Apr 4, 2018 at 0:33
  • What if I do the first and later realise it was inefficient, how do I save computational time without rerunning with the second option?
    – NelsonGon
    Jun 19, 2019 at 15:10
11

One way of doing that is by using trap DEBUG:

f() { bash -c "$BASH_COMMAND" >& /tmp/out.log; }
trap 'f' DEBUG

Now most recently executed command's stdout and stderr will be available in /tmp/out.log

Only downside is that it will execute a command twice: once to redirect output and error to /tmp/out.log and once normally. Probably there is some way to prevent this behavior as well.

4
  • 24
    so when I type rm -rf * && cd .., not only the current directory, but also the parent dir will be erased?? this approach seems highly dangerous to me
    – phil294
    Jul 4, 2017 at 15:58
  • 2
    Now how do I undo this action? Nov 13, 2018 at 10:25
  • 6
    @KartikChauhan: Just do trap '' DEBUG
    – anubhava
    Nov 13, 2018 at 14:13
  • Not a good idea to run a subshell for every command., Mar 3 at 13:52
11

Inspired by anubhava's answer, which I think is not actually acceptable as it runs each command twice.

save_output() { 
   exec 1>&3 
   { [ -f /tmp/current ] && mv /tmp/current /tmp/last; }
   exec > >(tee /tmp/current)
}

exec 3>&1
trap save_output DEBUG

This way the output of last command is in /tmp/last and the command is not called twice.

4
  • 3
    One downside I've found to this is that none of your commands think they are running in a tty anymore, which means you need special attention to get color codes working for utilities like grep or git diff +1 anyway though
    – Marty Neal
    May 16, 2018 at 17:41
  • 1
    This is the real answer
    – Luke Miles
    Nov 6, 2021 at 6:29
  • This is great!! The $BASH_COMMAND traps were running twice and also erasing my captures. Only thing is that with echo test it hangs until I press enter a second time. Any idea why? or if other commands will hang? Thanks!
    – alchemy
    Jun 28 at 4:20
  • unfortunately history also hangs until second Enter press, making this almost unusable until this can be resolved.
    – alchemy
    Jun 28 at 19:40
6

Yeah, why type extra lines each time; agreed. You can redirect the returned from a command to input by pipeline, but redirecting printed output to input (1>&0) is nope, at least not for multiple line outputs. Also you won't want to write a function again and again in each file for the same. So let's try something else.

A simple workaround would be to use printf function to store values in a variable.

printf -v myoutput "`cmd`"

such as

printf -v var "`echo ok;
  echo fine;
  echo thankyou`"
echo "$var" # don't forget the backquotes and quotes in either command.

Another customizable general solution (I myself use) for running the desired command only once and getting multi-line printed output of the command in an array variable line-by-line.

If you are not exporting the files anywhere and intend to use it locally only, you can have Terminal set-up the function declaration. You have to add the function in ~/.bashrc file or in ~/.profile file. In second case, you need to enable Run command as login shell from Edit>Preferences>yourProfile>Command.

Make a simple function, say:

get_prev() # preferably pass the commands in quotes. Single commands might still work without.
{
    # option 1: create an executable with the command(s) and run it
    #echo $* > /tmp/exe
    #bash /tmp/exe > /tmp/out
    
    # option 2: if your command is single command (no-pipe, no semi-colons), still it may not run correct in some exceptions.
    #echo `"$*"` > /tmp/out
    
    # option 3: (I actually used below)
    eval "$*" > /tmp/out # or simply "$*" > /tmp/out
    
    # return the command(s) outputs line by line
    IFS=$(echo -en "\n\b")
    arr=()
    exec 3</tmp/out
    while read -u 3 -r line
    do
        arr+=($line)
        echo $line
    done
    exec 3<&-
}

So what we did in option 1 was print the whole command to a temporary file /tmp/exe and run it and save the output to another file /tmp/out and then read the contents of the /tmp/out file line-by-line to an array. Similar in options 2 and 3, except that the commands were exectuted as such, without writing to an executable to be run.

In main script:

#run your command:

cmd="echo hey ya; echo hey hi; printf `expr 10 + 10`'\n' ; printf $((10 + 20))'\n'"
get_prev $cmd

#or simply
get_prev "echo hey ya; echo hey hi; printf `expr 10 + 10`'\n' ; printf $((10 + 20))'\n'"

Now, bash saves the variable even outside previous scope, so the arr variable created in get_prev function is accessible even outside the function in the main script:

#get previous command outputs in arr
for((i=0; i<${#arr[@]}; i++))
do
    echo ${arr[i]}
done
#if you're sure that your output won't have escape sequences you bother about, you may simply print the array
printf "${arr[*]}\n"

Edit:

I use the following code in my implementation:
get_prev()
{
    usage()
    {
        echo "Usage: alphabet [ -h | --help ]
        [ -s | --sep SEP ]
        [ -v | --var VAR ] \"command\""
    }
    
    ARGS=$(getopt -a -n alphabet -o hs:v: --long help,sep:,var: -- "$@")
    if [ $? -ne 0 ]; then usage; return 2; fi
    eval set -- $ARGS
    
    local var="arr"
    IFS=$(echo -en '\n\b')
    for arg in $*
    do
        case $arg in
            -h|--help)
                usage
                echo " -h, --help : opens this help"
                echo " -s, --sep  : specify the separator, newline by default"
                echo " -v, --var  : variable name to put result into, arr by default"
                echo "  command   : command to execute. Enclose in quotes if multiple lines or pipelines are used."
                shift
                return 0
                ;;
            -s|--sep)
                shift
                IFS=$(echo -en $1)
                shift
                ;;
            -v|--var)
                shift
                var=$1
                shift
                ;;
            -|--)
                shift
                ;;
            *)
                cmd=$option
                ;;
        esac
    done
    if [ ${#} -eq 0 ]; then usage; return 1; fi
    
    ERROR=$( { eval "$*" > /tmp/out; } 2>&1 )
    if [ $ERROR ]; then echo $ERROR; return 1; fi
    
    local a=()
    exec 3</tmp/out
    while read -u 3 -r line
    do
        a+=($line)
    done
    exec 3<&-
    
    eval $var=\(\${a[@]}\)
    print_arr $var # comment this to suppress output
}

print()
{
    eval echo \${$1[@]}
}

print_arr()
{
    eval printf "%s\\\n" "\${$1[@]}"
}

Ive been using this to print space-separated outputs of multiple/pipelined/both commands as line separated:

get_prev -s " " -v myarr "cmd1 | cmd2; cmd3 | cmd4"

For example:

get_prev -s ' ' -v myarr whereis python # or "whereis python"
# can also be achieved (in this case) by
whereis python | tr ' ' '\n'

Now tr command is useful at other places as well, such as

echo $PATH | tr ':' '\n'

But for multiple/piped commands... you know now. :)

-Himanshu

3

Like konsolebox said, you'd have to hack into bash itself. Here is a quite good example on how one might achieve this. The stderred repository (actually meant for coloring stdout) gives instructions on how to build it.

I gave it a try: Defining some new file descriptor inside .bashrc like

exec 41>/tmp/my_console_log

(number is arbitrary) and modify stderred.c accordingly so that content also gets written to fd 41. It kind of worked, but contains loads of NUL bytes, weird formattings and is basically binary data, not readable. Maybe someone with good understandings of C could try that out.

If so, everything needed to get the last printed line is tail -n 1 [logfile].

3

None of the other current answers are actual solutions, because they missed the point. Here’s how to read the output of commands

  1. after you’re ran them.
  2. Without executing them twice that may wreak havoc.
  3. Without any temporary files that use up your SSD.

The right question

First of all, you have to realize that your shell does not know any of the output. All answers focusing on that can be dismissed.
The commands, when executed, have three files that they can open and use. stdin, stdout, and stderr. Those are also available as /proc/self/[0-2] or /dev/pts/[0-2].
Those are pseudo-terminal slaves. Pseudo-terminals are essentially just named pipes, With a master and a slave end. The master end is just a file descriptor that a process gets, by opening /dev/pts/ptmx. (Which stands for pseudo-termunal master muxer.) … there used to be separate files for these somewhere in /dev/, and on true Unixes there still are (because everything is a file!), but GNU/Linux once again forget the lessons of its ancestors, and lost the power that came with them.

And so we end up with the place where the actual data resides that your processes put out, being in a buffer of your virtual terminal program. Be it a standard Linux virtual terminal console, or a X program like xterm or Konsole, or GNOME Terminal. Which then render it out via some graphical API.

Meaning, if you want to know those contents, you have to ask those programs!

The right answer

The standard interface for this is ANSI terminal commands. (You know, what lets you make text colorful, move the cursor, find the mouse pointer position or ever draw out pixels on the most modern terminals.
Unfortunately, I do not know of any terminal that supports accessing its buffer.
Plus there’s the difficulty of them all having different interfaces.

Fortunately, we can inject our own intermediate layer virtual terminal of our choice that supports accessing its buffer.
Which is a good idea to use as your login shell anyway, not only if you’re connecting via SSH and the connection might break mid-session. But also if you want to move your session somewhere else, e.g. to restart X.

How to actually do it, for real

The natural choice for this is of course something like screen. Which lets you log its entire session to a “file”. Which does not need to be a physical file, thanks to Unix principles. We can use a named pipe. And gather everything from there.

Outer part

Here’s how the “outer” part of the solution, that goes to the end of your .bashrc, could look like:

    if shopt -q login_shell && [[ $- == *i* ]]; then # Login + interactive?
      export ANS_FIFO=/tmp/$$.history ## $$ = current shell PID.
      mkfifo -m 600 $ANS_FIFO
      screen -OU -L -Logfile $ANS_FIFO # With optimal mode + UTF-8
      rm $ANS_FIFO # Clean up
    fi
Inner part

Now all the “inner” part has to do, is read $ANS_FIFO into $ANS after each command execution, to make it available as $ANS for following commands:

    export PROMPT_COMMAND+='[[ -v ANS_FIFO ]] && export ANS="$(< $ANS_FIFO)"'

It tells bash to run a command on each prompt, where it checks if $ANS_FIFO is set, and reads the file it is set to into $ANS. You can put this is your .bashrc too.

This technically works, and I have tested it (including escape sequences and UTF-8), BUT:

TO DO:

Problem: While this theoretically works, in practice, your shell will hang quickly, because a named pipe is not meant to be a buffer and is treated as synchronous (even though technically, on Linux, it does have a buffer, and you an even change its size programmatically), and so reading $ANS_FIFO when it’s empty, will wait for screen to write something (a line of text, presumably) to it, until it returns.

  • A possible solution for this would be, to have a rudimentary “listening server” (design pattern) in-between, that would be allowed to hang until it receives input, and set $ANS then. Combined with $ANS always being emptied first after a command, but before the listening server sets it and before the shell returns to the prompt, this would mean that $ANS is always set to only what screen logged since the last prompt, and reading it won’t lock you up.
  • Another, much better but harder solution, would be to add an interface to your terminal directly. And alter your shell to automatically provide its contents via the $ANS variable. Just like electronic calculators have been doing it for decades.

Unfortunately I have to go do other things now, since I sat on this for half the day. If anyone is willing to implement this before me, feel free to edit my answer. If you can’t, just add your own answer. I may or may not do it myself, next time I need this. :)

1
  • P.S.: Another way I just thought about: How about redirecting &1 to $ANS_FIFO right away, without using screen, by executing a [[ -v ANS_FIFO ]] && exec 1>$ANS_FIFO in your .bashrc Mar 3 at 15:46
1

Not sure exactly what you're needing this for, so this answer may not be relevant. You can always save the output of a command: netstat >> output.txt, but I don't think that's what you're looking for.

There are of course programming options though; you could simply get a program to read the text file above after that command is run and associate it with a variable, and in Ruby, my language of choice, you can create a variable out of command output using 'backticks':

output = `ls`                       #(this is a comment) create variable out of command

if output.include? "Downloads"      #if statement to see if command includes 'Downloads' folder
print "there appears to be a folder named downloads in this directory."
else
print "there is no directory called downloads in this file."
end

Stick this in a .rb file and run it: ruby file.rb and it will create a variable out of the command and allow you to manipulate it.

1
  • 9
    "Not sure exactly what you're needing this for" Well, say you (by which I mean I) just ran a long-duration script, want to search the output, and stupidly forgot to redirect the output before executing. Now I want to try to remedy my stupidity without re-running the script.
    – jscs
    Jul 31, 2017 at 15:03
1

If you don't want to recompute the previous command you can create a macro that scans the current terminal buffer, tries to guess the -supposed- output of the last command, copies it to the clipboard and finally types it to the terminal.

It can be used for simple commands that return a single line of output (tested on Ubuntu 18.04 with gnome-terminal).

Install the following tools: xdootool, xclip , ruby

In gnome-terminal go to Preferences -> Shortcuts -> Select all and set it to Ctrl+shift+a.

Create the following ruby script:

cat >${HOME}/parse.rb <<EOF
#!/usr/bin/ruby
stdin = STDIN.read
d = stdin.split(/\n/)
e = d.reverse
f = e.drop_while { |item| item == "" }
g = f.drop_while { |item| item.start_with? "${USER}@" }
h = g[0] 
print h
EOF

In the keyboard settings add the following keyboard shortcut:

bash -c '/bin/sleep 0.3 ; xdotool key ctrl+shift+a ; xdotool key ctrl+shift+c ; ( (xclip -out | ${HOME}/parse.rb ) > /tmp/clipboard ) ; (cat /tmp/clipboard | xclip -sel clip ) ; xdotool key ctrl+shift+v '

The above shortcut:

  • copies the current terminal buffer to the clipboard
  • extracts the output of the last command (only one line)
  • types it into the current terminal
0

I have an idea that I don't have time to try to implement immediately.

But what if you do something like the following:

$ MY_HISTORY_FILE = `get_temp_filename`
$ MY_HISTORY_FILE=$MY_HISTORY_FILE bash -i 2>&1 | tee $MY_HISTORY_FILE
$ some_command
$ cat $MY_HISTORY_FILE
$ # ^You'll want to filter that down in practice!

There might be issues with IO buffering. Also the file might get too huge. One would have to come up with a solution to these problems.

0

I think using script command might help. Something like,

  1. script -c bash -qf fifo_pid

  2. Using bash features to set after parsing.

0

Demo for non-interactive commands only: http://asciinema.org/a/395092

For also supporting interactive commands, you'd have to hack the script binary from util-linux to ignore any screen-redrawing console codes, and run it from bashrc to save your login session's output to a file.

0

Although the question is specifically for Bash, it may help users to know that in zsh the results of a command can inserted for execution as follows:

  1. $(my inner command here)
  2. press "TAB", the results of the command will be inserted
  3. Press enter to execute the command

A trivial example (press tab not enter after this line): $(echo "echo hello from inside")

-1

You can use -exec to run a command on the output of a command. So it will be a reuse of the output as an example given with a find command below:

find . -name anything.out -exec rm {} \;

you are saying here -> find a file called anything.out in the current folder, if found, remove it. If it is not found, the remaining after -exec will be skipped.

1
  • 1
    Downvoted, because -exec is just a find-specific option, not a generic construct. Also, there's nothing here to reuse find's or rm's output afterwards. Jan 26, 2022 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.