3

I tried these ways to append content to a file:

printf "\nsource ~/script.sh" >> /etc/bash.bashrc
echo -e "\nsource ~/script.sh" >> /etc/bash.bashrc
cat >> "/etc/bash.bashrc" <<< "source ~/script.sh"

All three ways work. I find the cat herestring less comfortable as one needs to read it from the end (after cat append this[end] to[start]).

All way don't automatically break the line and the \n syntax for printf and echo is one I'd personally use as last resort. Moreover, I don't know of any way to add a newline for an herestring, and found no such way in this documentation.

What I seek is a utility that goes down one line automatically without the need to add any argument or syntax. I tried to read on nix text processing utilities, but found nothing like what I seek. Do you know such a utility?

4

You don't need to write the cat line "the wrong way", this works just fine (though of course the arrows in the here-string still point in an odd direction):

$ cat <<< "some text" >> testfile
$ cat testfile
some text

As mentioned in the comments, the here-string adds a newline at the end, while with printf and echo you used \n in the front of the string. echo would usually add the tailing newline, just like the here-string, so if you need just that, echo should be fine (plus it's a builtin in most shells.)

All your commands work slightly differently with the newlines

cat >> file <<< "string"        # newline at end
printf "\nstring" >> file       # newline at beginning
echo -e "\nstring" >> file      # newline at beginning and at the end

Frankly, I think it's better to get used to the \n notation, but if you don't like it, there's always here-docs (both leading and tailing newlines here):

cat >> file <<EOF

some text
EOF
  • ilkkachu, is there a way to concatenate strings inside printf or echo, as in: printf "x" "y" >> ~/file? – user9303970 Feb 7 '18 at 11:43
  • @user9303970, well, echo concatenates the arguments with single spaces. With printf you could use something like printf "%s" this that to concatenate without a delimiter, or "%s " to add a space after each string, but of course printf doesn't add the trailing newline by itself. So I suppose it depends on what you want. – ilkkachu Feb 7 '18 at 11:50
2

If the file does end in a newline, as all correct POSIX text files must, there is no need to add a newline before the text you add to the file.

A way to make sure a file ends in a newline is to execute this:

$ sed -i -e '$a\' file

That will add a newline only if the file is missing the newline.

Then, you can add the text you need with a trailing new line to ensure the text file still ends in a newline:

a="source ~/script.sh"
echo  -e "$a" >> file    # with trailing new line
printf "$a\n" >> file    # with trailing new line
 <<< "$a" cat >> file    # with trailing new line

Note that the order of the cat command has been changed from the one you posted.

If you need an additional leading new line, use:

a="source ~/script.sh"
{ echo; echo  -e "$a" ; } >>file
echo -e "\n$a"            >>file
printf '%b' "\n$a\n"      >>file

a=$'\nsource ~/script.sh'
<<<"$a" cat >>file

If you want an additional newline with the cat option, use:

a=$'\nsource ~/script.sh\n'
<<<"$a" cat >>file

that will add two trailing new lines, one from the variable $a and one (unavoidable) from the <<< (here-string).

-1

Just because it is possible, here's a way to make append command in python:

#!/usr/bin/env python
import sys
with open(sys.argv[2],"a") as f:
     f.write(sys.argv[1] + "\n")

And works as so.

$ echo "first line" > input.txt
$ ./append  "Hello Unix.se" input.txt
$ cat input.txt 
first line
Hello Unix.se
  • This, however, appends the newline after the text, just as echo would. So what's the point? – Murphy Feb 7 '18 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.