I'm running Bash 5.1.4 on Debian.
I'm writing a post-installation script to copy configuration and other files to locations in my home directory. I add the intended destination to each file at the beginning with a prefix; for example: # DEST: $HOME/.config/mousepad/Thunar
(of course, in the script the file name will be substituted by a variable, and the hash symbol by the appropiate comment character; this line appears within the first 10 lines, not necessarily at the first, so I don't mess with shebangs).
To get these locations I'm using this command: head Thunar.acs | egrep "DEST:" | awk '{print $3}
, which returns literally $HOME/.config/Thunar
; I'd like it to expand $HOME
. What I mean is when I try ls $(head Thunar.acs | egrep "DEST:" | awk '{print $2})
I get the error ls: cannot access '$HOME/.config/Thunar/': No such file or directory
. I read this question and tried all of the combination of double quotes in the selected answer, but I still got the error. How can I solve this?
Enclosing the variable name in braces doesn't work either.
Thanks!
awk '/DEST:/ {print $3}; (NR>=10) {exit}' Thunar.acs
, what does it print? – Gordon Davisson Aug 15 '21 at 01:54(NR>=10)
still returns every third record of every line; also, using\$HOME
gives a warning, whereas using\\$HOME
doesn't. I deleted the commment out of shame, sorry. Thanks! – mathbekunkus Aug 15 '21 at 01:56gawk
(I havemawk
as default where I was testing); apparentlygawk
complains about\$HOME
, butmawk
allows it.\\$HOME
or[$]HOME
will work with either implementation. But I have no idea what'd make any version ofawk
print the third column of lines that don't contain "DEST:". – Gordon Davisson Aug 15 '21 at 02:23IFS
is set to something weird, your concern is a little misplaced here — if the pathname (in the file) contains whitespace, yourprint $3
already breaks it. If the “prefix” is known (predetermined), you could handle pathnames with whitespace with{ sub("# DEST: ", ""); sub("\\$HOME", home); print }
. Cases where the prefix isn’t predetermined can be handled with a little more work. … (Cont’d) – G-Man Says 'Reinstate Monica' Jan 22 '22 at 22:02$HOMEDRIVE
to/home/gordonDRIVE
. Obviously you could fix that withsub("\\$HOME/", home "/")
or equivalent. (3) User beware: if there are multipleDEST:
lines in the first ten lines, then you will get all the pathnames, concatenated, separated by newlines. You can fix that by addingexit
to the/DEST:/
action (after theprint
). – G-Man Says 'Reinstate Monica' Jan 22 '22 at 22:02