Bash, assigning command output to a variable [duplicate] - bash

This question already has an answer here:
Bash how do you capture stderr to a variable? [duplicate]
(1 answer)
Closed 3 years ago.
I am trying to automate the installation of nginx on multiple servers, and I have a shell script. It runs a version check if nginx is already installed and its version.
Trying to assign TMP=$(nginx -v), and instead of storing it in the variable it prints the results to console. printf "$TMP" prints an empty string

The problem is that your command does not print to STDOUT but to STDERR.
Using:
TMP=$(nginx -v 2>&1)
will solve your issue, see here for more details.

Related

ssh-add with argument - [duplicate]

This question already has answers here:
What's the magic of "-" (a dash) in command-line parameters?
(5 answers)
Closed 1 year ago.
I tried to add generated ssh id_rsa to the gitlab-ci.
Command I have found is echo "$SSH_PRIVATE_KEY" | ssh-add -.
I can not find any information about param - that was added to command ssh-add.
How can I interprate this?
In a man I have found information about running it without any argument and some flags but add single dash is not describe there.
I used ubuntu latest image for this process
As #Aaron said
It's a reference to the standard input of the command, so the output of the piped echo

`gcloud builds list` can't capture command output in variable [duplicate]

This question already has answers here:
Output not captured in bash variable [duplicate]
(1 answer)
why shell variable is not assigned to be the error messages from command used in command substitution? [duplicate]
(1 answer)
Closed 1 year ago.
I have some working code which follows the pattern seen below.
FIRST_BILLING_ACCOUNT=$(gcloud beta billing accounts list --filter=open=true --format="value(name)" --limit=1)
Check output with: $FIRST_BILLING_ACCOUNT
Prints my billing account's name.
And like this:
REGION=$(gcloud app describe --format="value(locationId.scope())")
Check output with: $REGION
Prints the region.
Problem
This does not work:
OUT=$(gcloud builds list --ongoing)
It prints the output: Listed 0 items. and does not save the value in $OUT.
Other things which do not work, pulled from here: How do I redirect output to a variable in shell?
OUT="$(gcloud builds list --ongoing)"
gcloud builds list --ongoing | read OUT
read OUT < <(gcloud builds list --ongoing)
I even tried this:
echo "gcloud builds list --ongoing" >> tst.sh
chmod +x tst.sh
OUT=$(./tst.sh)
$OUT
And it had the same result.
I want to know 2 things: How do I capture this command's output? And why do different gcloud commands seem to behave differently?
I just figured it out. It's printing to STDERR instead of STDOUT. To capture:
OUT=$(gcloud builds list --ongoing 2>&1)
I'll just have to use 2>&1 in all of my checks from now on.

why shell variable is not assigned to be the error messages from command used in command substitution? [duplicate]

This question already has answers here:
Output not captured in bash variable [duplicate]
(1 answer)
How to store standard error in a variable
(20 answers)
Closed 2 years ago.
I have the following:
a=$(grep pattern file_not_exist)
echo $a. #turns out a is empty
But I can see the grep complaining: grep: file_not_exist: No such file or directory.
Why is the error messages from grep not assigned to be the value of shell variable a? And if we want this kind of redirection, how to do it?
I am a shell green hand and just started. It seems stdout output are assigned to the shell variable. Could you point me to the documentation describing this kindly?
Thanks!
a will contain the output of the command returned to stdout. The error will be returned to sterr and so to get the error in the variable, you will need to redirect sterr to stout and so:
a=$(grep pattern file_not_exist 2>&1)
Here, 2 represent stdrr and 1 stdout.

bash: Run a command once and save stdout and stderr in their own variables [duplicate]

This question already has answers here:
Capture stdout and stderr into different variables
(21 answers)
Closed 2 years ago.
I need to ensure a command is outputting the correct text. I currently have:
command_stdout="$(mycommand --flag 2>/dev/null)"
command_stderr="$(mycommand --flag 2>&1 1>/dev/null)"
Instead of having to run the same command twice, is there any way I can run it once but still be able to save stdout and stderr's output to their appropriate variables?
Redirect stderr to a file, then set the second variable to the file contents.
command_stdout="$(mycommand --flag 2>/tmp/stderr.$$)"
command_stderr="$(</tmp/stderr.$$)"
rm /tmp/stderr.$$

How to print bash command output and save to file? [duplicate]

This question already has answers here:
How to store the output of a command in a variable at the same time as printing the output?
(4 answers)
Capture stdout to a variable but still display it in the console
(5 answers)
Closed 3 years ago.
I am writing bash script and inside I am executing a command. I want to save the output of the command to variable but also want to print the output of the command to the standard output. I dont want to print the variable once the command is completed. How can I achieve this ?
Try the following, it will print the output of your command and assign to variable.
VAR="$(your_command| tee /dev/tty)"

Resources