0

I'm in the middle of writing a config tool for a bluetooth application on Raspberry Pi with Stretch Lite. When I run hcitool scan I see this output:

Scanning ...
        00:80:52:51:3D:7E       BlueCN+001 D-513D7E
        84:C7:EA:64:45:87       Xperia X Compact

Now I want this output in a variable scanres, so I use

scanres=$(hcitool scan)

This is seems OK, but the mac-address misses the ':'

echo $scanres
Scanning ...
        00 80 25 51 3D 7E       BlueCN+001 D-513D7E
        84 C7 EA 64 45 87       Xperia X Compact

Somewhere in my script I changed the IFS (Internal Field Separator) without setting it back to the original value.

IFS=':'

Is this normal behaviour?

I know I can redirect the output to a file with hcitool scan>scanres but I want to avoid this if possible.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
SBF
  • 1,252
  • 3
  • 12
  • 21

1 Answers1

-1

Yes it is. You need to unset the IFS before calling echo

I assumed that xxx has a following content

Scanning ...
        00:80:52:51:3D:7E       BlueCN+001 D-513D7E
        84:C7:EA:64:45:87       Xperia X Compact

 IFS=':'
 ( IFS=''  var=$(cat xxx); echo $var; )
 echo "$IFS"

In return you will get

Scanning ...
        00:80:52:51:3D:7E       BlueCN+001 D-513D7E
        84:C7:EA:64:45:87       Xperia X Compact

and IFS is unchanged eq to ':'

slavik
  • 1,223
  • 15
  • 17
  • 1
    Emptying `IFS` prevents string-splitting, but it doesn't prevent glob expansion -- if your variable contained only the character `*`, then `echo $var` would still print a list of filenames instead of that `*`; if the variable contained `[some string]'` and you had a file named `s`, you'd get the file name printed, etc. The right answer here -- the thing that works in all cases -- is to quote the expansion. – Charles Duffy Aug 29 '17 at 15:14
  • You are right Charles ! Thanks for clarification :) – slavik Feb 16 '18 at 09:15