0

Running on the command line:

tmp=$(cd /sys/class/net; echo !(lo))
echo $tmp
eth0

Is OK. But running in script:

#!/bin/bash
tmp=$(cd /sys/class/net; echo !(lo))
echo $tmp

Outputs the error:

./test.sh: command substitution: line 3: syntax error near unexpected token '('
./test.sh: command substitution: line 3: 'cd /sys/class/net: echo !(lo))'

What is happening here? Why does it work correctly in the bash shell, but not in script?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
LightningWar
  • 915
  • 1
  • 19
  • 35
  • `shopt -s extglob` – Charles Duffy Aug 22 '18 at 14:28
  • BTW, lists should generally be stored in arrays, not string variables -- `shopt -s nullglob; devices=( !(lo) ); echo "Non-local devices:" >&2; printf ' - %q\n' "${devices[@]}"` is more robust than your original code against surprising device names -- and keep in mind that userspace code *can* rename devices. – Charles Duffy Aug 22 '18 at 14:30
  • dump this into [shellcheck.net](https://www.shellcheck.net/) and it may help out a bit. – JNevill Aug 22 '18 at 14:30
  • @JNevill, ...unfortunately, shellcheck doesn't appear to check for extglob use without explicit enablement. Sure, there are other issues, but the core one isn't caught. – Charles Duffy Aug 22 '18 at 14:31

0 Answers0