-1

I mean something like this

<{ any text; "with double quotes"; some $VARIABLE here; and 'single quotes'; and semicolons; } >| doc.txt

we have several quotes types and many nested variables to be solved.

we dont want to escape symbols.

with here-documents, you must have two lines.

with here-string, you must escape all nested quotes.

any logical solution ?

mr.tee
  • 39
  • 1
  • 5
  • 2
    Does this answer your question? [How does "cat << EOF" work in bash?](https://stackoverflow.com/questions/2500436/how-does-cat-eof-work-in-bash) – Aserre Jun 20 '22 at 11:44
  • my man. you go to 2 lines ... – mr.tee Jun 20 '22 at 12:03
  • 1
    Then you want a here-string: `cat > doc.txt <<< "your big string here"` -- you will have to escape any double quotes in the string, no avoiding that (except with a here-**doc** that you don't want) – glenn jackman Jun 20 '22 at 12:43
  • we have nested quotes ... – mr.tee Jun 20 '22 at 12:47
  • Why exactly do you want a oneliner ? There's no need for that if you use your command in a script. Are you using this in an interactive shell ? – Aserre Jun 20 '22 at 13:12
  • one liner aproche is better script performance :):) – mr.tee Jun 20 '22 at 15:50
  • 2
    I do not believe there is any evidence that multiline script has lower performance than single line. Rather the opposite, because of tokenization I would expect multiline scripts to perform indistinguishably better. `any logical solution ?` I do not understand, the logical solution is to use multiline lines and a here document, which is readable and maintainable in the long run. `we dont want to escape symbols.` Yet, you want `$VARIABLE` to expand. It is not possible - if you want `$VARIABLE` to expand, you have to have escape symbols for the case if you want `$VARIABLE` string preserved. – KamilCuk Jun 20 '22 at 15:59
  • Writting an instruction on 1 line or 2 does **not** change performance in any way. If you have a complex one-liner, you are better off splitting the instructions for better readability/maintainability – Aserre Jun 21 '22 at 08:08

1 Answers1

1

If the currently executed script is an actually existing file, you can get the current line and extract from it the text that you want to preserve, whereas the text may be stored as a comment after a special delimiter. With envsubst you can substitute variable expansions of exported variables. Something along:

sed -n "${BASH_LINENO}s/.*### //p" "$0" | VARIABLE="$VARIABLE" envsubst '$VARIABLE' > doc.txt  ### any text; "with double quotes"; some $VARIABLE here; and 'single quotes'; and semicolons;

You could also, in the same fashion, create an eval that would use that line content inside an eval-ed single line generated here-document:

eval "cat <<EOF$(echo)$(sed -n "${BASH_LINENO}s/.*### //p" "$0")$(echo EOF)" > doc.txt   ### any text; "with double quotes"; some $VARIABLE here; and 'single quotes'; and semicolons;

With base64 -w0 (or many other encoding) you can encode any script, also multiline, into a single line. Remove echo from below to actually execute the script.

echo eval "$(base64 -d <<<Y2F0IDw8RU9GCmFueSB0ZXh0OyAid2l0aCBkb3VibGUgcXVvdGVzIjsgc29tZSAkVkFSSUFCTEUgaGVyZTsgYW5kICdzaW5nbGUgcXVvdGVzJzsgYW5kIHNlbWljb2xvbnM7CkVPRgo=)" > doc.txt
KamilCuk
  • 120,984
  • 8
  • 59
  • 111