1

how to assign echo to variable in bash

I have simple command:

echo "foo=bar" | cut -d "=" -f1

to cut some string from some string.

How can I assign this result to some variable?

Nothing work

(echo "foo=bar" | cut -d "=" -f1)
$test=`(echo "foo=bar" | cut -d "=" -f1)`
$test=`$(echo "foo=bar" | cut -d "=" -f1)`
$test="$(echo "foo=bar" | cut -d "=" -f1)"

Output:

foo
script.sh: 14: =foo: not found
script.sh: 1: foo: not found
script.sh: 15: =: not found
script.sh: 16: =foo: not found

Submitted March 17th 2021 by Admin

Answers
0

You can use $() (command substitution):

test=$(echo "foo=bar" | cut -d "=" -f1)
echo "$test"
foo

Admin | 7 months ago



Relevant Questions