Shell tricks - Programming On Unix

Users browsing this thread: 1 Guest(s)
josuah
Long time nixers
Inserting raw data in a script

How to embed a text object in a shell script?

You can use quotes:

Code:
printf 'Oh noes
this "\t" won't look like a backslash and a "t"!'

Two problems:

- The ' often used in various languages clashes with the quotes.

Plan 9's rc shell uses '' to mean ' within the quoted string: 'It''s great and easy on the eyes'

- printf(1) (like echo, sometimes...) interprets the \t.

Code:
printf '\\t\n'     # not convenient
printf '%s\n' '\t' # a little bit better but still...
printf '%s\n' "'single quote' but what about $this"
cat << EOF         # har, har...
$(rm -rf /home)
EOF
cat << 'EOF'       # wait what? /home is still here and I see $(rm -rf /home)
$(rm -rf /home)
EOF

The last one is our solution: you can quote 'EOF', and this stops all expansion on the following text. So you can have any text including mixed ' and " quotes along with $, \ and whatnot.

And yes, that is POSIX.


Messages In This Thread
Shell tricks - by josuah - 13-11-2017, 05:27 AM
RE: Shell tricks - by josuah - 13-11-2017, 05:39 AM
RE: Shell tricks - by venam - 14-11-2017, 01:53 AM
RE: Shell tricks - by josuah - 23-11-2017, 07:59 AM
RE: Shell tricks - by josuah - 23-11-2017, 08:52 AM
RE: Shell tricks - by josuah - 04-12-2017, 07:43 AM
RE: Shell tricks - by z3bra - 04-12-2017, 08:11 PM
RE: Shell tricks - by josuah - 05-12-2017, 08:07 AM
RE: Shell tricks - by josuah - 05-12-2017, 09:02 AM
RE: Shell tricks - by josuah - 05-12-2017, 09:08 AM
RE: Shell tricks - by josuah - 08-12-2017, 12:25 PM