You indeed have nested syntax errors, probabily arising from a confusion about variable expansion and command substitution.
What you want to achieve is command substitution, i.e. you want to assign to the variable DEBFILE
the output of a command. This is done as follows:
DEBFILE=$( your command )
The syntax ${ ... }
you used in your original attempt is instead parameter/variable expansion, where you "dereference" the shell variable whose name is placed in the curly braces. Note that ${VARIABLE}
is basically the same as $VARIABLE
, so for the first command you showed, your shell would try to look up a variable whose name is (lynx -listonly -dump https://gridtracker.org/release.html) | grep .deb | grep nwjs | awk '{ print $2 }'
and assign its value to DEBFILE
.(1)
The correct way to achieve your goal should simply be
DEBFILE=$(lynx -listonly -dump https://gridtracker.org/release.html | grep .deb | grep nwjs | awk '{ print $2 }')
and you could probably replace the grep
-grep
-awk
pipeline by a single awk
command, which would probably look like
DEBFILE=$(lynx -listonly -dump https://gridtracker.org/release.html | awk '/\.grep/ && /nwjs/ { print $2 }')
or
DEBFILE=$(lynx -listonly -dump https://gridtracker.org/release.html | awk '(index($0,".deb") && index($0,"nwjs")) { print $2 }')
Note that the code you labelled as "This works" basically does exactly what I proposed, just with an unnecessary echo
and the intermediate step of writing the output to a file from which it is read back to the variable $DEBFILE
:
echo $(lynx ... | grep .deb | grep nwjs)
has the same effect as lynx ... | grep .deb | grep nwjs
(but could possibly introduce unwanted reformatting of whitespace) because it basically makes the console output of the lynx ... | grep ... | grep ...
pipeline an argument to echo
, which will simply print it to the console (again).
- So,
echo $(lynx ... | grep .deb | grep nwjs) | awk '{ print $2 }'
does the same as lynx ... | grep .deb | grep nwjs | awk '{ print $2 }'
(1) As noted by Stéphane Chazelas, ksh93
actually does support command substitution in a ${ cmd; }
form, but it requires the command to end in a ;
, it is not POSIX-compatible, and Bash doesn't support it.
debfile=$(lynx -listonly -dump https://gridtracker.org/release.html | grep -Pom1 '\S*nwjs\S*\.deb$') || die "Can't determine nwjs URL"
– Stéphane Chazelas Nov 03 '22 at 08:51