script01.sh works just fine
#!/bin/sh -x
egrep 'snmp.* version [123]|ip route ' $1
output
[user@linux ~]$ script01.sh file.txt
+ egrep 'snmp.* version [123]|ip route ' file.txt
However, when I changed the content and put it in variable, it's break.
script02.sh
#!/bin/sh -x
var='snmp.* version [123]|ip route '
egrep $var $1
output script02.sh
[user@linux ~]$ script02.sh file.txt
' x='snmp.* version [123]|ip route
+ egrep 'snmp.*' version '[123]|ip' route $'\r' file.txt
egrep: version: No such file or directory
egrep: [123]|ip: No such file or directory
egrep: route: No such file or directory
: No such file or directory
When I double quoted both $var
& $1
, this is happen.
script03
#!/bin/sh -x
var='snmp.* version [123]|ip route '
egrep "$var" "$1"
output script03
[user@linux ~]$ script03.sh file.txt
' var='snmp.* version [123]|ip route
' file.txt
$var
and$1
needs to be quoted – Inian Jan 21 '20 at 09:34