Just a stupid question. Provide a code snippet
b="a=2"
How to extract the value 2 and assign it to variable a
You could just eval the code..
eval $b
I am renowned to give ugly
solutions so I won't dissapoint you -
[jaypal:~/Temp] a=$(awk '{print $(NF-1)}' FS='["=]' <<< 'b="a=2"')
[jaypal:~/Temp] echo $a
2
Less intense
solution
[jaypal:~/Temp] a=$(awk -F= '{print $NF}' <<< $b)
[jaypal:~/Temp] echo $a
2
a=${b#a=}
Take the value of $b
, remove the leading text a=
; assign what's left (2
) to a
.
$ b="a=2"
$ var=${b##*=}; echo $var
2
If you are looking for a shell utility to do something like that, you can use the cut command.
To take your example, try:
echo "abcdefg" | cut -c3-5
Where -cN-M tells the cut command to return columns N to M, inclusive.
REF: What linux shell command returns a part of a string? and Extract substring in Bash