Whitespace treatment in OS X vs Linux in Bash

1

I am trying to run a very simple script on a few different platforms:

#!/bin/bash
STRING="this is a test"
echo $STRING

, and I cannot tell why on Mavericks and Yosemite I get the output:

./starter.sh line 2: is": command not found

while an online Bash terminal simulator (sorry, I don't have any Linux setup handy, so I am using: this) runs the code as expected. According to Apple's own primer docs this should work. IOW, double-quotes are simply ignored.

A few points:

  • there are no spaces in-between variable name, equals operator and assigned value;
  • escaping spaces is not a valid answer in my case, as the end goal is to make some more complicated scripts...but obviously I must "graduate" this small step first.

elder elder

Posted 2015-08-10T20:12:18.617

Reputation: 306

I think you have a hidden control character or something after the is. This works perfectly fine on my macs running Yosemite. Open up the file in vim and type :set list. – yoonix – 2015-08-10T20:39:38.930

Thank you, @yoonix, I was sure my whitespaces were right, but this deff. steered me in the right direction. – elder elder – 2015-08-11T09:39:12.060

Answers

2

The error message isn't quite right, but it looks a lot like what you'd get if the script had fancy (or "smart") double-quotes instead of plain (ASCII) double-quotes. The shell doesn't understand the fancy quotes, so they'll make the script behave in very weird ways. Try printing out your script with:

LC_ALL=c cat -vet starter.sh

This will show any weird (non-plain-ASCII or nonprinting) characters in the file. If it prints something like:

#!/bin/bash$
STRING=?M-^@M-^\this is a test?M-^@M-^]$
echo $STRING$

... the "?M-^@M-^\" and "?M-^@M-^]" bits are fancy quotes in UTF-8 encoding. On the other hand, if the file's lines end with "^M$" instead of just "$", then Sleafar is right and you've got a DOS/Windows formatted file.

Gordon Davisson

Posted 2015-08-10T20:12:18.617

Reputation: 28 538

1

Thank you, Gordon Davisson, that was it! Seeing the comments from the other two users, I dug a little bit further and found about the "fancy quotes", which is, IMHO, a somewhat unfortunate idea from Apple to enable by default. (Thank you all for steering me in the right direction.) This: http://apple.stackexchange.com/questions/120486/quotes-problem-in-mavericks-or-textedit/171855#171855 might help anyone looking into disabling this mechanism.

– elder elder – 2015-08-11T09:38:00.457

1

Check what is used as the "end of line" in your file. Different systems use different control characters:

  • Linux: LF
  • Windows: CR+LF
  • Mac: CR

You can't use the same script on all systems without converting it.

Sleafar

Posted 2015-08-10T20:12:18.617

Reputation: 176

That is a very good point, even if it didn't apply in my case; appreciated, especially since your comment as well gave me some clues, later confirmed by Gordon's answer. – elder elder – 2015-08-11T09:40:28.297