In Linux shell bash script, how to print to a file at the same line? In Linux shell bash script, how to print to a file at the same line? shell shell

In Linux shell bash script, how to print to a file at the same line?


After wading through that question, I've decided that what you're looking for is echo -n.


If you are looking for a single tab in between the variables, then printf is a good choice.

printf '%s\t%s' "$v1" "$v2" >> file_name

If you want it exactly like your example where the tab is padded with a space on both sides:

printf '%s \t %s' "$v1" "$v2" >> file_name


few options there:

  1. echo -n foo bar It's simple, but may not work on some old UNIX systems like HP-UX or SunOS. Instead the "-n" will be printed as well as the rest of the arguments followed by new line.
  2. echo -e "foo bar\c" . The \c has meaning: "produce no further output". I don't like this solution personally, but some UNIX wizards use it.
  3. printf %b "foo bar" I like this solution the most. It's quite portable as well flexible.