How can I append the string :/etc/simmmerin/bin/ into a file filename using echo command onto an existing row. Can't get it working.

echo ":/etc/simmerin/bin/" >> filename

It just appends in the bottom of the file instead of continuing on the row I wanted it to be. I would like to use echo instead of going into each files in vim or similar.


The file already has a newline at the end, terminating the final line. Anything append to it, therefore comes on another line. You'll need to overwrite the final newline, which you can't do with just a >> redirection. Probably easiest to use sed:

$ seq 1 4 | sed -e '$s,$,stuff/to/append,'

To have sed modify a file in-place, use

sed -i -e '$s,$,stuff/to/append,' filename

(The dollar signs have two meanings there. The first $ is an address that controls what line the next command runs, here it means the last line. The second $ is regex notation for the end of line.)

To append to some other line than the last, replace the first $ with either a line number or a pattern match. This would append a string to any line containing foo:

sed -i -e '/foo/ s,$,stuff/to/append,' filename

Note that I used commas here as separators for the s command. Slashes would be more common, but the text to be added contains them. The s command allows (almost) any character to be used.

If you have slashes in the pattern used to find the correct line, you can use something like \,foo, as the address, i.e. start with a backslash and use whatever separator is convenient.

| improve this answer | |
  • is is sed or seq? seq 1 4 | sed -e? or sed 1 4 | sed -e? – johnsnow May 22 '18 at 8:14
  • @johnsnow, ah, sorry, that's confusing. seq is a utility that outputs a sequence of numbers. I only used it here to produce some input. sed is the stream editor that does the work. I added the command to have sed directly modify a file. – ilkkachu May 22 '18 at 8:18
  • Cant get it working if I wanted to specify a row for it to append onto. – johnsnow May 22 '18 at 8:30
  • @johnsnow, ah, yes, I thought you meant the last. You'll need to add a condition before the s, edited – ilkkachu May 22 '18 at 9:44

If you want to modify an existing file, you can use sed command.

Given that you have both your string to append, and the line number you want to insert the string:


Insert the string at the beginning of the line:

sed "${lineno}s,^,${mystring}," file

Insert the string at the end of the line:

sed "${lineno}s,\$,${mystring}," file

Insert the string before the line:

sed "${lineno}s,^,${mystring}\n," file

Insert the string after the line:

sed "${lineno}s,\$,\n${mystring}," file
| improve this answer | |

Your file already has a newline at the end of file. Newline character is 1 byte large: use the truncate command to shrink the file by one byte and then append at the end of file.

truncate -s -1  filename && echo ":/etc/simmerin/bin/" >> filename
| improve this answer | |

instead of echo use printf, this one do not add "\n" by default

please note that printf is a command of bash, so make sure your shell support it

| improve this answer | |
  • This does not really answer the question. When should they use printf? If the last line of the file contains a newline at the end, using printf will not solve anything. Also, all shells on a POSIX system understands printf. – Kusalananda May 22 '18 at 9:19

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.