Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

bash

shell

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

At each iteration,

I used

 echo "$variable1"  >> file_name, 

 echo "$variable2"  >> file_name, 

but echo insert a newline so that it becomes

 $v1

 $v2 

not

     $v1 \tab  $v2

"\c" cannot eat newline.

this post BASH shell script echo to output on same line

does not help .

thanks

like image 410
user1002288 Avatar asked Jan 08 '12 23:01

user1002288


People also ask

How do I print on the same line in Linux?

2.1. We can use printf command to display the text to the standard output stream. The output of the command and the next command prompt are on the same line. No newline is printed.

How do I print a file line by line in bash?

Syntax: Read file line by line on a Bash Unix & Linux shell The syntax is as follows for bash, ksh, zsh, and all other shells to read a file line by line: while read -r line; do COMMAND; done < input. file.

How do you print a loop output in a single line in a shell script?

Using printf or echo -n . Also, try to use start=$(($start + 1)) or start=$[$start + 1] instead of back ticks to increment the variable. Save this answer.


2 Answers

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

like image 83
Ignacio Vazquez-Abrams Avatar answered Sep 28 '22 14:09

Ignacio Vazquez-Abrams


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
like image 44
jordanm Avatar answered Sep 28 '22 16:09

jordanm