Redirect content under a new line (without further syntax or arguments)

6,176

Solution 1

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

Solution 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).

Share:
6,176

Related videos on Youtube

user9303970
Author by

user9303970

Updated on September 18, 2022

Comments

  • user9303970
    user9303970 over 1 year

    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?

  • Murphy
    Murphy about 6 years
    This, however, appends the newline after the text, just as echo would. So what's the point?
  • ilkkachu
    ilkkachu about 6 years
    @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.