echo $DISPLAY
won't work. If it did, you wouldn't need to set or export DISPLAY
in the first place. You'll need to find out what the appropriate value is using some other way.
If you want to find out what DISPLAY
your user is currently running, try:
w -h $USER | awk '$2 ~ /:[0-9.]*/{print $2}'
Then you can do:
export DISPLAY=$(w -h $USER | awk '$2 ~ /:[0-9.]*/{print $2}')
w
lists the currently logged-in users and where they are logged in from, with DISPLAY
being used for users logged in by GUI.
- With
awk
, we match the second field, the location, to something that looks like a DISPLAY
and print it out.
In general there are more appropriate ways of parsing JSON objects, but since in this case the JSON object is very simple you may store curl
's output in a variable (which is possible) and just use AWK:
var="$(curl ipinfo.io/"8.8.8.8" 2>/dev/null)"
<<<"$var" awk -F'"' '$2=="city"{printf("%s, ", $4)}$2=="region"{print $4}'
% var="$(curl ipinfo.io/"8.8.8.8" 2>/dev/null)"
% <<<"$var" awk -F'"' '$2=="city"{printf("%s, ", $4)}$2=="region"{print $4}'
Mountain View, California
However unless you want to use curl
's output multiple times you may just use a pipe:
curl ipinfo.io/"8.8.8.8" 2>/dev/null | awk -F'"' '$2=="city"{printf("%s, ", $4)}$2=="region"{print $4}'
curl ipinfo.io/"8.8.8.8" 2>/dev/null | awk -F'"' '$2=="city"{printf("%s, ", $4)}$2=="region"{print $4}'
Mountain View, California
<<<
is a form of input redirection called "here string"; it redirects the STDIN of a command from the terminal to a string.
What happens here is that $var
is expanded between the double quotes; the STDIN of the AWK command is redirected from the terminal to the expanded string and AWK consequently reads the string as its input file.
Best Answer
To get
$SECONDS
into HH:MM:SS format you will need to do some (integer) math: