My use case is as follows.
file.txt
First arg: %s
Second %s arg,
Third %s
Run prog file.txt "one" "$(ls dir1/dir2)" "three"
Resulting in
First arg: one
Second file1 file2 file3 arg,
Third three
I first thought to use sed but that inevitably runs into problems when bash substitutions get read as format characters like this sed s/%1/$VAR/g
-> sed s/%1/dir1/dir2/file1/g
I’m thinking I could iterate by lines and take the text before and after a substitute and just insert like
FIRST=$(sed "s/%s.*//" <<< $LINE) SECND=$(sed "s/.*%s//" <<< $LINE) echo "$FIRST $SUB $SECND"
Though that’s limited to one sub per line and I would prefer a cleaner solution if it exists.
Advertisement
Answer
You could use printf
with the whole file as the formatting string:
printf "$(< file.txt)n" one two three
Notice that command substitution removes trailing newlines, so a separate n
is added back; see How to avoid bash command substitution to remove the newline character? for details.