1

This seems like such a simple fix that I'm a bit embarrassed to ask, but I'm having trouble getting a variable to resolve in a shell command:

read -r -p "SIP Peer name (e.g. ATAx1): " peer
status=`sudo asterisk -rx 'sip show peer $peer' | grep -i "status"`

I tried surrounding $peer with quotes like "$peer" as well, but the result is the same:

SIP Peer name (e.g. ATAx1): ATAx1
SIP Peer status for ATAx1:
Press ENTER to return:

For kicks, I tried hardcoding it in like so:

status=`sudo asterisk -rx 'sip show peer ATAx1' | grep -i "status"`

And voila!

SIP Peer name (e.g. ATAx1): ATAx1
SIP Peer status for ATAx1:   Status       : UNKNOWN

It works! Yes, the status is supposed to be "Unknown". But more importantly, why is the variable $peer not working here?

Sometimes I copy and paste syntax and forget to match the variable generated by the read command with that actually used in the shell command, but that is not the case here.

Are you not allowed to use variables in these statements? Or more likely, is some special syntax required? I can't single quote obviously, and I tried double quoting. Is there a syntax error here causing the variable to not get used in the command?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
InterLinked
  • 1,247
  • 2
  • 18
  • 50
  • 2
    See: [Difference between single and double quotes in bash](http://stackoverflow.com/q/6697753/3776858) – Cyrus Jun 27 '19 at 13:23

1 Answers1

2

When you do, as you did,

sudo asterisk -rx 'sip show peer $peer'

The single quotes are telling the shell not to interpolate variable references and similar. If you do it in double quotes, it should work:

sudo asterisk -rx "sip show peer $peer"

You had the right guess - to use double quotes - but you added it in the wrong place.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
Nadav Har'El
  • 11,785
  • 1
  • 24
  • 45