I always do this to append text to a file
echo "text text text ..." >> file
# or
printf "%s\n" "text text text ..." >> file
I wonder if there are more ways to achieve the same, more elegant or unusual way.
I always do this to append text to a file
echo "text text text ..." >> file
# or
printf "%s\n" "text text text ..." >> file
I wonder if there are more ways to achieve the same, more elegant or unusual way.
I quite like this one, where I can set up a log file at the top of a script and write to it throughout without needing either a global variable or to remember to change all occurrences of a filename:
exec 3>> /tmp/somefile.log
...
echo "This is a log message" >&3
echo "This goes to stdout"
echo "This is written to stderr" >&2
The exec 3>dest
construct opens the file dest
for writing (use >>
for appending, <
for reading - just as usual) and attaches it to file descriptor #3. You then get descriptor #1 for stdout, #2 for stderr, and this new #3 for the file dest
.
You can join stderr to stdout for the duration of a script with a construct such as exec 2>&1
- there are lots of powerful possibilities. The documentation (man bash
) has this to say about it:
exec [-cl] [-a name] [command [arguments]]
Ifcommand
is specified, it replaces the shell. [...] Ifcommand
is not specified, any redirections take effect in the current shell [...].
Here are few other ways to append text to a file.
Using tee
tee -a file <<< "text text text ..."
Using awk
awk 'BEGIN{ printf "text text text ..." >> "file" }'
Using sed
sed -i '$a text text text ...' file
sed -i -e "\$atext text text ..." file
Sources:
Using a here-document
approach:
cat <<EOF >> file
> foo
> bar
> baz
> EOF
Tests:
$ cat file
aaaa
bbbb
$ cat <<EOF >> file
> foo
> bar
> baz
> EOF
$ cat file
aaaa
bbbb
foo
bar
baz
See dd(1) man page:
dd conv=notrunc oflags=append bs=4096 if=myNewData of=myOldFile
dd conv=notruct oflag=append of=destination <<<'Some string of text'
– Chris Davies
Feb 14 '20 at 15:42
<<<
herestring construct. I have no idea what dd
would do in those cases, would it be safe?
– terdon
Feb 15 '20 at 16:04
dd
from any other file descriptor (but I've not checked the source). On that assumption, I would expect dd
to be as safe as with any other similar invocation. echo 'Some string of text or printf if you prefer' | dd conv=notrunc oflag=append of=destination
, etc.
– Chris Davies
Feb 15 '20 at 16:12
<<<
, it should be perfectly safe. I just wonder what happens with shells that don't. Will dd
simply get no input and, if so, not write anything? Will it write an empty string? Or a NULL? I guess it would do nothing, but I was just idly wondering since I don't know.
– terdon
Feb 15 '20 at 16:14
dash
and other POSIX shells does not have a herestring
only a heredoc
so it stands to reason that if dd
accepts input from stdin
it should work with a heredoc
on POSIX shells, see https://paste.opensuse.org/view/raw/14083760
– Jetchisel
Feb 15 '20 at 21:04
<<<
– Jetchisel
Feb 15 '20 at 21:11
dd <<'EOF' >> file
literal newlinehere
'text here
EOF` is enough.
– Jetchisel
Feb 15 '20 at 23:05
Using the Unix file editors. Both GNU
and BSD
version.
Using ed(1)
with printf
printf '%s\n' '$a' 'foo bar baz' . w | ed -s file.txt
The bash specific but more cryptic syntax using the $' '
shell quoting and a herestring
ed -s file.txt <<< $'$a\nfoo bar baz\n.\nw'
Using ex(1)
with printf
printf '%s\n' '$a' 'foo bar baz' . x | ex -s file.txt
The bash specific but more cryptic syntax $' '
shell quoting and a herestring
ex -s file.txt <<< $'$a\nfoo bar baz\n.\nx'
cat >> file
first line
second line
...
last line
Hit Enter at the last line then Ctrl + D.
echo "a\nb"
will print something different thanprintf "%s\n" "a\nb"
, in ~all the shells but bash. You cannot assume that the two are similar. – Feb 14 '20 at 05:47(a ; b ; c) >> file
– Thorbjørn Ravn Andersen Feb 15 '20 at 13:44