Shell Script - save variable substring

A really simple shell script question. I have a file with something like this:

var "aaaaa"
var "bbbbb"
...

Now, how can I assign a quoted string to a variable? This is what I have (but I miss the destination part ...):

while read line
do
  echo $line | cut -d" " -f3
done

which prints what I want ... how to save it in a variable?

thank

+3
source share
3 answers
my_var=$(echo $line | cut -d" " -f3)

You need to execute the command. This is what $ () is for.

+7
source

you don’t need to use the external cut command to β€œcut” the lines. You can use a shell that is more efficient.

while read -r a b
do
  echo "Now variable b is your quoted string and has value: $b"
done <"file"

or

while read -r line
do
  set -- $line
  echo $2
done <"file"
+1
source

.

Bourne ,

my_var_name=expression

,

setenv my_var_name expression

(, "cut xyz" ), backticks :

my_var_name=`echo $line | cut -d" " -f3`

I think bash also supports $ (), but I'm not sure about the difference from backticks - see section 3.4.5 from http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html

0
source

Source: https://habr.com/ru/post/1735473/


All Articles