Bash – printf output running over into next line

bash-scriptprintf

I have created Bash SS in vi. My output is running over into the line below it. I am new to UNIX so I'm trying to learn. My output is displayed using printf.

printf "%-15s %15s %15s %2d\n %2s " $name $days $phone $start $time

The output looks like this for example

name       days       phone      start 

time name    days       phone      start

time name    days       phone      start 

etc...

How do I get all five of my variables to print on same line?

Best Answer

Your command:

printf "%-15s %15s %15s %2d\n %2s " $name $days $phone $start $time

Your problem:

'...\n %2s'

You're inserting a newline before $time. Stop that. Do:

printf '%-15s %15s %15s %2d %2s\n' \
    "$name" "$days" "$phone" "$start" "$time"