Shell script, saving the command value to a variable
use backticks (or $()
– can be nested), not single quotes:
VARI=`cat filename | head -1 | cut -d, -f${i}` # or:
VARI=$(cat filename | head -1 | cut -d, -f${i})
How to store an output of shell script to a variable in Unix?
Two simple examples to capture output the pwd
command:
$ b=$(pwd)
$ echo $b
/home/user1
or
$ a=`pwd`
$ echo $a
/home/user1
The first way is preferred. Note that there can't be any spaces after the =
for this to work.
Example using a short script:
#!/bin/bash
echo "hi there"
then:
$ ./so.sh
hi there
$ a=$(so.sh)
$ echo $a
hi there
In general a more flexible approach would be to return an exit value from the command and use it for further processing, though sometimes we just may want to capture the simple output from a command.
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.
Store value to variable in Shell script
You have to use the right quotation, like this:
total_rows=`echo "$emails" | ./jq '.total_rows'`
The `` will execute the command and give total_rows
the value of it, so whatever would be the output of
echo "$emails" | ./jq '.total_rows'
will so be stored in total_rows
.
As mentioned in the comments by Tom Fenech, it is better to use $()
for command substitution. It provides a better readability. So what you can do is:
total_rows=$(echo "$emails" | ./jq '.total_rows')
How can I store a command in a variable in a shell script?
Use eval:
x="ls | wc"
eval "$x"
y=$(eval "$x")
echo "$y"
shell script can not save output from command line into variable
You have a space before your equals sign:
out =`python --version`
Should be:
out=`python --version`
Update
Also python outputs the version string to stderr, so you need to redirect it to stdout:
out=`python --version 2>&1`
Shell: save ssh command result to local variable
Your command should be wrapped in "()" not "{}" when assigning fetching the result to a variable. Also, the others are just variables not commands so don't need a wrapper (assuming they are defined in a script or something).
lines=$(ssh $user@$hostname "wc -l < $workspace/logs")
Related Topics
Mixing Static Libraries and Shared Libraries
How to Find Words from One File in Another File
How to Get the List of Dependent Child Images in Docker
Readelf VS. Objdump: Why Are Both Needed
Pass Output as an Argument for Cp in Bash
How to Query X11 Display Resolution
How to Build a Docker Image on Windows 10
Concatenating Two String Variables in Bash Appending Newline
Does The Linux Scheduler Needs to Be Context Switched
Shell Prompt That Is Based on Location in Filesystem
How to Pass Shell Variables as Command Line Argument to a Shell Script
How to Make an "Alias" for a Long Path
Remove Odd or Even Lines from a Text File
How to Remove the Win10's Path from Wsl
Aws-Ec2, How to Set Multiple Public Sites with Just One Instance