-1

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 works:

(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
Maroun
  • 94,125
  • 30
  • 188
  • 241
vimov32802
  • 89
  • 2
  • 5
  • Why down vote? What is wrong with my question? Downvote without any comment? :-/ what should I improve? – vimov32802 Mar 17 '21 at 07:58
  • If you're actually trying to get the part of a variable before an "`=`", you don't need to use `echo` or `cut`, just built-in variable substitution: `var="foo=bar"; test="${var%%=*}"`. See [the bash manual on parameter expansion](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html), and look for the section on `${parameter%%word}`. – Gordon Davisson Mar 17 '21 at 09:21
  • @vimov32802 : A downvote usuall means that you didn't have put enough of your own effort in it. From your question, it looks to me that you did not search on the Net for the problem - as you can see,this topic has been discussed already many times on [so], and even more often on other websites - but just tried out a few commands, without really knowing what you are doing. – user1934428 Mar 17 '21 at 10:23

1 Answers1

3

You can use $() (command substitution):

test=$(echo "foo=bar" | cut -d "=" -f1)
echo "$test"
foo
Maroun
  • 94,125
  • 30
  • 188
  • 241