ChatGPT解决这个技术问题 Extra ChatGPT

Capturing multiple line output into a Bash variable

I've got a script 'myscript' that outputs the following:

abc
def
ghi

in another script, I call:

declare RESULT=$(./myscript)

and $RESULT gets the value

abc def ghi

Is there a way to store the result either with the newlines, or with '\n' character so I can output it with 'echo -e'?

it surprises me. don't you have $(cat ./myscipt) ? otherwise i would have expected it to try to execute commands abc, def and ghi
@litb: yes, I suppose so; you can also use $(<./myscript) which avoids executing a command.
(NB: the two comments above refer to a revision of the question that started I've got a script 'myscript' that contains the following, which led to the questions. The current revision of the question (I've got a script 'myscript' that outputs the following) makes the comments superfluous. However, the revision is from 2011-11-11, long after the two comments were made.

J
Jonathan Leffler

Actually, RESULT contains what you want — to demonstrate:

echo "$RESULT"

What you show is what you get from:

echo $RESULT

As noted in the comments, the difference is that (1) the double-quoted version of the variable (echo "$RESULT") preserves internal spacing of the value exactly as it is represented in the variable — newlines, tabs, multiple blanks and all — whereas (2) the unquoted version (echo $RESULT) replaces each sequence of one or more blanks, tabs and newlines with a single space. Thus (1) preserves the shape of the input variable, whereas (2) creates a potentially very long single line of output with 'words' separated by single spaces (where a 'word' is a sequence of non-whitespace characters; there needn't be any alphanumerics in any of the words).


@troelskn: the difference is that (1) the double-quoted version of the variable preserves internal spacing of the value exactly as it is represented in the variable, newlines, tabs, multiple blanks and all, whereas (2) the unquoted version replaces each sequence of one or more blanks, tabs and newlines with a single space. Thus (1) preserves the shape of the input variable, whereas (2) creates a potentially very long single line of output with 'words' separated by single spaces (where a 'word' is a sequence of non-whitespace characters; there needn't be any alphanumerics in any of the words).
To make the answer easier to understand: the answer tells that echo "$RESULT" preserves newline, while echo $RESULT does not.
This fails to preserve newlines and leading spaces in some situations.
@CommaToast: Are you going to elaborate on that? Trailing newlines are lost; there isn't an easy way around that. Leading blanks — I'm not aware of any circumstances under which they're lost.
J
Jonathan Leffler

Another pitfall with this is that command substitution$() — strips trailing newlines. Probably not always important, but if you really want to preserve exactly what was output, you'll have to use another line and some quoting:

RESULTX="$(./myscript; echo x)"
RESULT="${RESULTX%x}"

This is especially important if you want to handle all possible filenames (to avoid undefined behavior like operating on the wrong file).


I had to work for a while with a broken shell that did not remove the last newline from the command substitution (it is not process substitution), and it broke almost everything. For example, if you did pwd=`pwd`; ls $pwd/$file, you got a newline before the /, and enclosing the name in double quotes didn't help. It was fixed quickly. This was back in 1983-5 time frame on ICL Perq PNX; the shell didn't have $PWD as a built-in variable.
u
user2574210

In case that you're interested in specific lines, use a result-array:

declare RESULT=($(./myscript))  # (..) = array
echo "First line: ${RESULT[0]}"
echo "Second line: ${RESULT[1]}"
echo "N-th line: ${RESULT[N]}"

If there are spaces in the lines, this will count fields (contents between spaces) rather than lines.
You would use readarray and process substitution instead of command substitution: readarray -t RESULT < <(./myscript>.
L
Lurchman

In addition to the answer given by @l0b0 I just had the situation where I needed to both keep any trailing newlines output by the script and check the script's return code. And the problem with l0b0's answer is that the 'echo x' was resetting $? back to zero... so I managed to come up with this very cunning solution:

RESULTX="$(./myscript; echo x$?)"
RETURNCODE=${RESULTX##*x}
RESULT="${RESULTX%x*}"

This is lovely, I actually had the use case where I want to have a die () function that accepts an arbitrary exit code and optionally a message, and I wanted to use another usage () function to supply the message but the newlines kept getting squashed, hopefully this lets me work around that.
F
F. Hauri - Give Up GitHub

Parsing multiple output

Introduction

So your myscript output 3 lines, could look like:

myscript() { echo $'abc\ndef\nghi'; }

or

myscript() { local i; for i in abc def ghi ;do echo $i; done ;}

Ok this is a function, not a script (no need of path ./), but output is same

myscript
abc
def
ghi

Considering result code

To check for result code, test function will become:

myscript() { local i;for i in abc def ghi ;do echo $i;done;return $((RANDOM%128));}

1. Storing multiple output in one single variable, showing newlines

Your operation is correct:

RESULT=$(myscript)

About result code, you could add:

RCODE=$?

even in same line:

RESULT=$(myscript) RCODE=$?

Then

echo $RESULT $RCODE
abc def ghi 66

echo "$RESULT"
abc
def
ghi

echo ${RESULT@Q}
$'abc\ndef\nghi'

printf "%q\n" "$RESULT"
$'abc\ndef\nghi'

but for showing variable definition, use declare -p:

declare -p RESULT RCODE
declare -- RESULT="abc
def
ghi"
declare -- RCODE="66"

2. Parsing multiple output in array, using mapfile

Storing answer into myvar variable:

mapfile -t myvar < <(myscript)
echo ${myvar[2]}
ghi

Showing $myvar:

declare -p myvar
declare -a myvar=([0]="abc" [1]="def" [2]="ghi")

Considering result code

In case you have to check for result code, you could:

RESULT=$(myscript) RCODE=$?
mapfile -t myvar <<<"$RESULT"

declare -p myvar RCODE
declare -a myvar=([0]="abc" [1]="def" [2]="ghi")
declare -- RCODE="40"

3. Parsing multiple output by consecutives read in command group

{ read firstline; read secondline; read thirdline;} < <(myscript)
echo $secondline
def

Showing variables:

declare -p firstline secondline thirdline
declare -- firstline="abc"
declare -- secondline="def"
declare -- thirdline="ghi"

I often use:

{ read foo;read foo total use free foo ;} < <(df -k /)

Then

declare -p use free total
declare -- use="843476"
declare -- free="582128"
declare -- total="1515376"

Considering result code

Same prepended step:

RESULT=$(myscript) RCODE=$?
{ read firstline; read secondline; read thirdline;} <<<"$RESULT"

declare -p firstline secondline thirdline RCODE
declare -- firstline="abc"
declare -- secondline="def"
declare -- thirdline="ghi"
declare -- RCODE="50"

Look about help read and help mapfile for options regarding newlines, backslashes and other specialities!!
C
Community

After trying most of the solutions here, the easiest thing I found was the obvious - using a temp file. I'm not sure what you want to do with your multiple line output, but you can then deal with it line by line using read. About the only thing you can't really do is easily stick it all in the same variable, but for most practical purposes this is way easier to deal with.

./myscript.sh > /tmp/foo
while read line ; do 
    echo 'whatever you want to do with $line'
done < /tmp/foo

Quick hack to make it do the requested action:

result=""
./myscript.sh > /tmp/foo
while read line ; do
  result="$result$line\n"
done < /tmp/foo
echo -e $result

Note this adds an extra line. If you work on it you can code around it, I'm just too lazy.

EDIT: While this case works perfectly well, people reading this should be aware that you can easily squash your stdin inside the while loop, thus giving you a script that will run one line, clear stdin, and exit. Like ssh will do that I think? I just saw it recently, other code examples here: https://unix.stackexchange.com/questions/24260/reading-lines-from-a-file-with-bash-for-vs-while

One more time! This time with a different filehandle (stdin, stdout, stderr are 0-2, so we can use &3 or higher in bash).

result=""
./test>/tmp/foo
while read line  <&3; do
    result="$result$line\n"
done 3</tmp/foo
echo -e $result

you can also use mktemp, but this is just a quick code example. Usage for mktemp looks like:

filenamevar=`mktemp /tmp/tempXXXXXX`
./test > $filenamevar

Then use $filenamevar like you would the actual name of a file. Probably doesn't need to be explained here but someone complained in the comments.


I tried other solutions too, with your first suggestion I finally got my script working
Downvote: This is excessively complex, and fails to avoid multiple common bash pitfalls.
Ya someone told me about the weird stdin filehandle problem the other day and I was like "wow". Lemme add something really quickly.
In Bash, you can use the read command extension -u 3 to read from file descriptor 3.
In addition to @JonathanLeffler comment, you could write: while read -ru $testout line;do echo "do something with $line";done {testout}< <(./test) instead of creating temporary file!
R
Rahul Reddy

How about this, it will read each line to a variable and that can be used subsequently ! say myscript output is redirected to a file called myscript_output

awk '{while ( (getline var < "myscript_output") >0){print var;} close ("myscript_output");}'

Well, that's not bash, that's awk.