0

Show wireless network interface:

$ sudo iwconfig | grep wlp4s0 
lo        no wireless extensions.

enp3s0 no wireless extensions.

nekoray-tun no wireless extensions.

wlp4s0 IEEE 802.11 ESSID:"Redmi_A6F0"

Why is the result not just one line? Expected result:

wlp4s0    IEEE 802.11  ESSID:"Redmi_A6F0"  
phuclv
  • 30,396
  • 15
  • 136
  • 260
showkey
  • 291

1 Answers1

3

You shouldn't use grep for that. Just run sudo iwconfig wlp4s0 for information about that specific interface

Back to your question, the reason for what you saw is because iwconfig writes to both stdout and stderr. You can easily see that by disabling one of the streams

$ sudo iwconfig 2>/dev/null     # Prints only stdout
wlo2      IEEE 802.11  ESSID:off/any
          Mode:Managed  Access Point: Not-Associated
          Retry short limit:7   RTS thr:off   Fragment thr:off
          Encryption key:off
          Power Management:on

$ sudo iwconfig 1>/dev/null # Prints only stderr lo no wireless extensions.

enp69s0 no wireless extensions. ...

That's why these commands from Nasir Riley's answer work, because stderr is redirected into stdout so that grep/sed/awk can read on that stream

iwconfig 2>&1 | grep wlp4s0
iwconfig 2>&1 | sed -n '/wlp4s0/p'
iwconfig 2>&1 | awk '/wlp40/ {print}'

echo $(iwconfig 2>&1) > file is completely wrong, because without double quotes the shell always does word splitting, which removes all the newline characters. That means the output of iwconfig is entirely concatenated and separated by spaces is also wrong, because the above commands already confirms that iwconfig output contains multiple lines

echo "$(iwconfig 2>&1)" > file will work, but it'll fall into the useless use of echo, similar to the useless use of cat. Just use sudo iwconfig >file 2>&1 or sudo iwconfig &>file

phuclv
  • 30,396
  • 15
  • 136
  • 260