0

Currently I have this two commands separeted by ;:

ID=$(wmctrl -d | grep "\* DG"); kdialog --msgbox "D = ${ID:(-1)}"

Is there away to circumvent the creation of the auxiliary variable ID but still use substring indexing (here used with negative index to count from behind)?

I am looking for some replacement for XXX in "D = ${XXX:(-1)}". My current attempts all lead to "bad substitution" error.

Note: The original command (below) had some flaws to which some of the comments refer:

export ID=$(wmctrl -d | egrep "\* DG"); kdialog --msgbox "D = ${ID:(-1)}"
cknoll
  • 2,130
  • 4
  • 18
  • 34
  • http://stackoverflow.com/questions/30146466/command-substitution-with-string-substitution – ewcz Mar 19 '17 at 13:47
  • 3
    No, but you don't need to export the variable. – chepner Mar 19 '17 at 13:54
  • The regex would work fine with basic `grep` also. We would replace `egrep` with `grep -E` these days, but the `-E` option is completely superfluous here. – tripleee Mar 19 '17 at 16:57

1 Answers1

1

An obvious workaround is to use sed instead of egrep:

kdialog --msgbox "D = $(wmctrl -d | sed -n 's/.*\* DG.*\(.\)$/\1/p')"

The logic here is to not print any lines by default (-n); then on the line matching the regex, substitute the entire line with just its last character, and print when there was a match and subsequent substitution (/p).

tripleee
  • 175,061
  • 34
  • 275
  • 318