0

using:

#!/bin/bash
echo "Please, enter name"
read NAME
echo "$NAME," >> MyFile$(date +%m%d%Y)
echo "done"

it will append the NAME to the file without issue, however, I want it to append the existing line in the file rather than starting a new line with each append

For example:

bob,sarah,jane,

instead of what I have currently happeneing which is:

bob,
sarah,
jane,
1

Try echo -n ..... It usually allows to output string without end of line mark.

echo -n "$NAME," >> MyFile$(date +%m%d%Y)
  • While echo usually works, it's not very portable. Different versions of echo use different means for suppressing the newline. A more portable solution is to use printf, e.g., printf "%s" "$NAME," >> Myfile$(date +%m%d%Y). – garyjohn Aug 20 '15 at 22:36
  • 1
    @Augamire: If this is what you wanted, then your question wasn't very clear.  Do you realize that this solution will permanently maintain your file in a state of not ending with a newline?  That might cause programs not to handle it correctly. – G-Man Says 'Reinstate Monica' Aug 20 '15 at 23:32
  • @G-Man I apologize if my question is unclear. I am very new to bash. Does the next echo statement not create the ending with a newline? What would your recommendation be for a solution to the challenge I face, if not the -n option? – Augamire Aug 24 '15 at 14:20
  • What "next echo statement"?  The echo "done"?  That goes to the script's standard output (stdout); I presume that that is the terminal, or at least something other than YourFile.  The question suggests (to me) that you were looking for something like sed -i "\$s/\$/$NAME,/" "MyFile$(date +%m%d%Y)". – G-Man Says 'Reinstate Monica' Aug 24 '15 at 15:10
  • @G-Man I think I see what you are getting at. In this case, I am actually appending to an existing netgroup config where I do not want to end with a newline, as the line will be appended to frequently & a new line will, as I underdstand it, actually break the environment without other changes also being executed. – Augamire Aug 24 '15 at 15:34

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.