For text content, the easiest, especially if the text contains multiple lines is to use a here-document. Add these lines to your script for instance for your script to create a
file.txt with this text:
cat << 'EOF' > file.txt
Here is some text
(you can use anything in place of
EOF above as the end of file marker provided it doesn't occur in the text to insert. The first
EOF has to be quoted to prevent any expansion inside the here-document).
For files with arbitrary content, you can use
uudecode. It also lets you specify the permissions for the file being created. For instance for a script to create a
foo file with the content and permissions of
(echo 'uudecode << "_EOF_"'
uuencode -m /bin/ls foo
echo _EOF_) >> your-script
Which will add the correspond code to your script. Something like:
uudecode << "_EOF_"
begin-base64 755 foo
With the content of
/bin/ls encoded in base64.
That assumes the
uudecode is available on the machine you're going to run that command in. But that should generally be the case as
uudecode is a POSIX command.
You can extend that to create several files by encoding a compressed tar archive:
(echo 'uudecode << "_EOF_" | gunzip | tar xpf -'
tar cf - file1 file2 file3 dir/file4 | uuencode -m -
echo '_EOF_') >> your-script
(assuming the target system has
tar (not POSIX commands but quite common)).
That also has the benefit of letting you encode additional file metadata like ownership or access and modification time (and more like ACLs and extended attributes, and hard link relationship with some
tar implementations). It also allows you to easily create non-regular files like directories, symlinks, devices...
See also the
shar utility which can help you create portable self-extractable archives.