Here is output of:
sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"\(.*\)".*/\1/' 2>/dev/null
Yash Shah CoreFragment CoreFragment_5G CoreFragment dlink Yash Shah COMFAST Appbirds_Technologies SYMBIOSIS 20096641 CoreFragment_5G AMBER_AP1 REDWING LABS_5G
While the same thing written in a script is not working the same.
Here is a snippet in which I used above command.
for ssid_name in $(sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"\(.*\)".*/\1/' 2>/dev/null) do echo "$ssid_name" done
I got output like this:
Yash Shah CoreFragment CoreFragment_5G CoreFragment Yash Shah REDWING LABS COMFAST Appbirds_Technologies SYMBIOSIS CoreFragment_5G REDWING LABS_5G
Note: When there is a space in output it take as a new line.
I'm working on Ubuntu 18.04.