Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
You put the \n line after the last arg. –  mikeserv Apr 14 at 2:05
    
This is not a typo - the asker deliberately used malformed syntax because he/she was unaware of how to properly form the command. This should be reopened and should never have been closed. –  mikeserv Apr 14 at 14:38
add comment

2 Answers 2

up vote 7 down vote accepted

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"
share|improve this answer
1  
Thank you so much. That worked like a charm. For some reason I thought that \n was for all numbers. I should have used the printf manual. Thanks again! –  user65158 Apr 14 at 13:19
add comment

In addition to @mikeserv's answer, you can see the complete list of format controls for output from man 1 printf:

   \"     double quote    
   \\     backslash    
   \a     alert (BEL)    
   \b     backspace    
   \c     produce no further output    
   \e     escape    
   \f     form feed    
   \n     new line    
   \r     carriage return    
   \t     horizontal tab    
   \v     vertical tab    
   \NNN   byte with octal value NNN (1 to 3 digits)    
   \xHH   byte with hexadecimal value HH (1 to 2 digits)    
   \uHHHH Unicode (ISO/IEC 10646) character with hex value HHHH (4 digits)    

   \UHHHHHHHH
          Unicode character with hex value HHHHHHHH (8 digits)    

   %%     a single %    
   %b     ARGUMENT as a string with `\' escapes interpreted, except that 
          octal escapes are of the form \0 or \0NNN

You should read man <command name> to learn how to use command in any *nix OS.

share|improve this answer
    
man answers are my favorite answers. Note that for printf in particular, though, the majority of information is included in the C-specific printf(3) man page as opposed to the shell-specific printf(1) man page. –  mikeserv Apr 14 at 2:32
    
@mikeserv: What do you mean "opposed"? I think printf(1) is only a short documentation of printf(3), not "opposed". –  Gnouc Apr 14 at 2:38
    
I meant majority as opposed to minority. Sorry if i was unclear. I upvoted you anyway. Definitely one should start with printf(1) - but because its just a carry-over of C's print function it's expected youll go looking in its docs for more info. –  mikeserv Apr 14 at 2:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.