i want to get the result of the following command into a variable.
xprop -name "google-chrome-stable" | grep "window id" | awk '{print $5}' | awk '{print $1}'
the result should look something like that
OUTPUT=xprop -name "google-chrome-stable" | grep "window id" | awk '{print $5}' | awk '{print $1}'
echo $OUTPUT
I know it could be done with command substitution. The examples in the Link command substitution are not detailed enough because they dont explain how to escape the quotation marks and apostrophes. Could someone help me find out how to solve that?
It looks like you simply need to do this:
output=$(xprop -name "google-chrome-stable" | awk '/window id/{print $5}')
echo "$output"
There's no need to do any escaping within the $( )
. I've wrapped the variable to be echoed in double quotes to prevent things like glob expansion from occurring. As a bonus, I've combined your grep and awk commands and removed the last one which wasn't doing anything useful.
I have also made your variable name lower case. Upper case variable names should be reserved for shell internals. You'll thank me for this one day when you have a variable called $PATH
and suddenly everything stops working...