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
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With