How do I set a variable to the output of a command in Bash?
In addition to backticks `command`
, command substitution can be done with $(command)
or "$(command)"
, which I find easier to read, and allows for nesting.
OUTPUT=$(ls -1)
echo "${OUTPUT}"
MULTILINE=$(ls \
-1)
echo "${MULTILINE}"
Quoting ("
) does matter to preserve multi-line variable values; it is optional on the right-hand side of an assignment, as word splitting is not performed, so OUTPUT=$(ls -1)
would work fine.
how do I store the output of a cut -c command into a variable in shell script
Try
my_var="$( echo $filename | cut -c 15-20 )"
Demo:
$filename=Incoming_file_180420053826.csv
$my_var="$( echo $filename | cut -c 15-20 )"
$echo $my_var
180420
$
How to store the output of a command in a variable at the same time as printing the output?
Use tee to direct it straight to screen instead of stdout
$ var=$(echo hi | tee /dev/tty)
hi
$ echo $var
hi
Print command output while storing in variable
output=$(ginkgo -r -cover | tee /dev/fd/2)
You can use tee to send stdout to both stdout and stderr. stdout is captured into your variable, stderr is printed.
bash store output as a variable
Use command substitution:
my_var=$(grep -A 26 "some text" somefile.txt |
awk '/other text/ { gsub(/M/, " "); print $4 }' |
sort -n -r | uniq | head -n1)
Also, for portability, I would suggest always using -n1
for the argument of head
. I've come across a couple of incarnations of it where using -1
doesn't work.
Storage commands output as a variable in bash
You appear to be confusing storing the output of commands with storing the text of the commands themselves (which is almost always a bad idea). I'm not sure exactly what you're trying to do (or what the output of nas_server -list all
looks like), but I suspect you want something like this:
nameserver="$(nas_server -list all | awk '{print $6}')" # $() captures the output of a command
echo "$nameserver" # Double-quote all variable references to avoid parsing weirdness!
nameserverreal="$(echo "$nameserver" |awk '/encap|nameserver_/{ print }')"
echo "$nameserverreal"
Here's a simplified version:
nameserverreal="$(nas_server -list all | awk '$6 ~ /encap|nameserver_/ {print $6}'"
Oh, and anytime you're tempted to use eval
in a shell script, it's a sign that something has gone horribly wrong.
how to store large output in a variable in bash script
To catch the standard output of a program, do something like
pout=$(prog)
To catch the standard error of a prorgram, do
perr=$(prog 2>&1 >/dev/null)
To catch both into different variables, do
errout=tmp.$$
pout=$(prog 2>$errout)
perr=$(<$errout)
rm $errout
This gives you the respective output as a scalar. If you want to have it in an array, you do something like
pout=( $(prog) )
etc.
How do i store the output of a bash command in a variable?
PROCESS=$(echo "$LINE" | awk '{print $2}')
or
PROCESS=$(ps aux | grep "$1" | awk '{print $2}')
I don't know why you're getting the error you quoted. I can't reproduce it. When you say this:
PROCESS=$LINE | awk '{print $2}'
the shell expands it to something like this:
PROCESS='mayoff 10732 ...' | awk '{print $2}'
(I've shortened the value of $LINE
to make the example readable.)
The first subcommand of the pipeline sets variable PROCESS
; this variable-setting command has no output so awk
reads EOF immediately and prints nothing. And since each subcommand of the pipeline runs in a subshell, the setting of PROCESS
takes place only in a subshell, not in the parent shell running the script, so PROCESS
is still not set for later commands in your script.
(Note that some versions of bash
can run the last subcommand of the pipeline in the current shell instead of in a subshell, but that doesn't affect this example.)
Instead of setting PROCESS
in a subshell and feeding nothing to awk
on standard input, you want to feed the value of LINE
to awk
and store the result in PROCESS
in the current shell. So you need to run a command that writes the value of LINE
to its standard output, and connects that standard output to the standard input of awk
. The echo
command can do this (or the printf
command, as chepner pointed out in his answer).
Related Topics
Multiplication with Expr in Shell Script
How to Get Multi-Line String Between Two Braces Containing a Specific Search String
Remove Redundant Paths from $Path Variable
Is \D Not Supported by Grep's Basic Expressions
How to Add .So File to the Java.Library.Path in Linux
How to Copy Multiple Files from a Different Directory Using Cp
Set-Up X11 Forwarding Over Ssh
Add Margin to PDF File When Merging Using Pdftk or Similar
Linux Kernel: What Process Does Schedule() Run In
Monitoring File and Directory Access on Linux
How to Run Script Commands from Variables
What Is Partition Checker in Arm Secure Mode
How to Extract the Contents of an Rpm
Portable Way to Get File Size (In Bytes) in the Shell
How to Find Ports Opened by Process Id in Linux
How to Align the Columns of a Space Separated Table in Bash