Author Topic: How to get a line in array in bash and use every word as variable from it  (Read 1310 times)

gouravjoshi

  • Administrator
  • Full Member
  • *****
  • Posts: 94
  • Karma: 1
Hi,

While working on script I found a good way to get a whole line from a file as set of variables. Many time we have a line and we need to get its contents as variables. For example -

We have a line "This is my file"

Now I can have it as
LINE="This is my file"

 set - ${LINE// / }

Now
$1 = This
$2 = is
$3 = my
$4 = file

or say I have an IP Address 192.168.0.1

IP=192.168.0.1

set - ${IP//./ }

Now
$1 = 192
$2 = 168
$3 = 0
$4 = 1

Hope this will be helpful for many of us.