Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.
function runstatus {
    echo "SVN Status -uq:"
    echo 
    status=$(svn status -uq)
    echo $status
    echo $bigline
}

This prints something like:

SVN Status -uq:

rd_backup rd_backup.sh update 764 update Status against revision: 771

Whereas normally, manually running the command instead of ./update, it looks like:

svn status -uq

M * 764 update

Status against revision: 771

ie with newlines. How can I make sure the formatting stays the same, and is legible, when running from a script?

share|improve this question
2  
Why not just run svn status -uq instead of echoing the output of $(svn status -uq)? –  slhck Apr 23 '13 at 11:44
    
Oh yeah that does seem to make a lot more sense actually –  paimoe Apr 23 '13 at 11:46

1 Answer 1

up vote 4 down vote accepted

If you want a function to just show the output of a command, there's no need to capture the output and then echo it again.

Just run svn status -uq directly. The stdout will be output from the function call as well.


If you really need to use variables and echo them, you need to quote the argument passed to echo if you want the whitespace to be preserved: echo "$status".

The reason is that $status will literally contain the following:

M * 764 update
Status against revision: 771

When you run echo $status, echo will be called like this:

echo M * 764 update \
Status against revision: 771

This is more than one parameter, since Bash splits arguments on whitespace (see $IFS). echo will print each argument, separated by one space, so while the rest of the line looks fine, the newline will – in essence – be collapsed to one space.

If you surround the argument with a quote, echo will only see it as one argument and correctly output it again.

The Bash Wiki has a good read on quotes and lists a few reasons why you should always quote your variables apart from a few exceptions.

share|improve this answer
    
See "Word Splitting" in the bash manual: gnu.org/software/bash/manual/bashref.html#Word-Splitting –  glenn jackman Apr 24 '13 at 1:31

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.