we set the variable with the follwing values
MY_VAR=" one two three four"
how to capture the first value from variable with bash? ( without using echo command )
expected output
one
This is more appropriate for StackOverflow... in fact it's been asked many times before!
Inspiration from this answer.
$ MY_VAR=" one two three four"
$ read -ra MY_VAR <<< "${MY_VAR}"
$ echo ${MY_VAR[0]}
one
$ echo ${MY_VAR[2]}
three
Or (from this answer):
$ MY_VAR=" one two three four"
$ MY_VAR=(${MY_VAR// / })
$ echo ${MY_VAR[0]}
one
This should work:
echo $MY_VAR | awk '{print $1;}'