0

How to save a line with spaces without line breaks in a file? Such as this:

Value2="Server=server1.windows.net,1433;Database=DB;Persist Security Info=False;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"

using:

printf '%s\n' $Value2 >> "./print.csv"

At present it gets saved as:

Server=server1.windows.net,1433;Database=DB;Persist
Security
Info=False;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection
Timeout=30;
agc
  • 7,973
  • 2
  • 29
  • 50
JhnWorks
  • 3
  • 3
  • There is no newline after `Persist` from **that command you have posted**. Look at the created file with a hex editor. Also, your created file contains double quotes which are not present in your creation command. Some other program must have got its fingers in it. – user1934428 Oct 29 '21 at 12:32
  • The output acquired some cruft, probably when the Q. was being written. I've replaced the output with the output code would actually produce, which is more consistent with the Q.'s text. – agc Oct 29 '21 at 22:48
  • Why `${Value2}`? The curly braces do nothing useful here. `$Value2` would have behavior identical to what you have now, whereas `"$Value2"` would have the behavior you _actually want_. – Charles Duffy Oct 29 '21 at 22:49

1 Answers1

0

The variable $Value2 isn't quoted, and has two separate spaces, so the printf command sees three strings, not one -- so printf prints three strings on three lines. To make printf see just one string, put the variable in quotes, like this:

printf '%s\n' "${Value2}"

Output:

Server=server1.windows.net,1433;Database=DB;Persist Security Info=False;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;

Or, as proof use wc to count the lines:

printf '%s\n' "${Value2}" | wc -l

Output:

1
agc
  • 7,973
  • 2
  • 29
  • 50