Returning print carriage in OSX bash

Why echo- does carriage return from an OSX terminal behave differently than from a bashscript?

From the terminal in OSX 10.7.3:

$ echo $SHELL
/bin/bash

$ /bin/bash --version
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin11)
Copyright (C) 2007 Free Software Foundation, Inc.

$ echo -ne "hello\rbye"
byelo

But I see a different result from test.sh:

#!/bin/bash
echo -ne "hello\rbye"

... running test.shgives me:

$ ./test.sh
byehello

I was expecting byelo. Why is it different? and how to fix it?

+3
source share
2 answers

This is somehow related to #!/bin/shthe top of my script. After I changed it to #!/bin/bash, I saw the expected result.

+1
source

I just ran the same on my Mac and got the same results.

I think two possibilities:

  • One of your settings set -oor shootcan do it
  • .bashrc ( script) - .

:

$ echo -ne "hello\rbye"
bye$
$ test.sh   #Shell script with the one line in it
buy$ []

[] . $PS1="$ ".

, printf, .

$ printf "hello\rbye"

printf CR, - .

+1

All Articles