0
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?

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

1 Answer 1

4

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.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .