num=$(gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings | grep -oE '[0-9]+'|sort -rn|head -n 1)
key=$(gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings)
newnum=$(grep -oE '[0-9]+' $key |sort -rn|head -n 1)
echo $newnum

grep takes the $key as filename, but I want it to search the text stored in the variable $text.

  • 2
    echo "${key}" | grep ... Mar 12, 2018 at 4:37
  • @AndyDalton , imo this is the solution that OP is looking for and should be put as an answer , not a comment
    – amisax
    Mar 12, 2018 at 5:13

1 Answer 1


You can use herestring <<< to pass the string to grep:

newnum=$(grep -oE '[0-9]+' <<< "$key" | sort -rn | head -n 1)

That would be more efficient than echo "$key" | grep which would create an extra process.

Remember to enclose your variables in double quotes to prevent word splitting and globbing. See this as well.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .