94

Doing some stream editing to change the nasty Parallels icon. It's poorly developed and embedded into the app itself rather than being an image file. So I've located this sed command that has some good feedback:

sudo sed -i.bak s/Parallels_Desktop_Overlay_128/Parallels_Desktop_Overlay_000/g /Applications/Parallels\ Desktop.app/Contents/MacOS/prl_client_app

It returns sed: RE error: illegal byte sequence

Can anyone explain what this means? What part of the command is the problem?

Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94
alt
  • 13,357
  • 19
  • 80
  • 120

4 Answers4

162

Try setting the LANG environment variable (LANG=C sed ...) or use one of the binary sed tools mentioned here: binary sed replacement

Why the error?

Without LANG=C sed assumes that files are encoded in whatever encoding is specified in LANG and the file (being binary) may contain bytes which are not valid characters in LANG's encoding (thus you could get 'illegal byte sequence').

Why does LANG=C work?

C just happens to treat all ASCII characters as themselves and non-ASCII characters as literals.

Community
  • 1
  • 1
ldrg
  • 4,150
  • 4
  • 43
  • 52
  • 4
    If `bash` is your shell, you can enter `export LANG=C` and try again. – user664833 Nov 30 '12 at 22:21
  • 29
    Great, but using `LC_ALL=C sed ...` is the more robust approach: if `LC_ALL` or `LC_CTYPE` are set (to something other than `C`), setting `LANG` will have no effect. (`LC_ALL` _overrides_ all individually set categories, if any, whereas `LANG` only takes effect for those categories _not_ explicitly set.) – mklement0 May 11 '14 at 14:22
  • 9
    To me `LANG=C` did not work but `LC_ALL=C` did – mreferre Dec 10 '15 at 08:07
76

LANG=C alone didn't do the trick for me but adding LC_CTYPE=C as well solved it.

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
Malström
  • 869
  • 6
  • 5
  • Same for me on OSX 10.8. Thanks! – offner Sep 26 '13 at 14:33
  • 12
    An _effective_ value of `LC_CTYPE=C` is sufficient to solve the problem: `LC_CTYPE=C sed ...`. However, that won't work if `LC_ALL` is set (to something other than `C`), because that _overrides_ all individual `LC_*` categories. Thus, the most robust approach is to use `LC_ALL=C sed ...`. – mklement0 May 11 '14 at 14:17
  • This worked for me on macOS Sierra – alec_djinn Dec 10 '18 at 09:13
29

In addition to LANG=C and LC_CTYPE=C, I had to do LC_ALL=C to get this to work.

LC_ALL overrides all individual LC_* categories. Thus, the most robust approach is to use LC_ALL=C sed ... - no need to also deal with the other variables.

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
rjpeter2
  • 399
  • 3
  • 3
  • explain it a little bit more, please – rpax May 07 '14 at 17:06
  • 2
    @rpax: Actually, an effective `LC_CTYPE` value of C is sufficient, so using `LC_CTYPE=C sed ...` (directly prepending to the offending command) is _normally_ sufficient, _unless_ `LC_ALL` - which _overrides_ all individual `LC_*` categories - has been set. Thus, the most robust approach is to use `LC_ALL=C sed ...` - no need to also deal with the other variables. – mklement0 May 11 '14 at 14:12
  • This should be the accepted answer. Hope you don't mind, I edited @mklement0's comment into the answer. – Qix - MONICA WAS MISTREATED Sep 03 '16 at 22:41
  • @Qix: My contribution was added as a _comment_ because I deemed it too invasive to be an edit: your edit amounts to putting words into @rjpeter2's mouth, and, in its current form, results in a somewhat self-contradictory answer. Do note that I've recommended the `LC_ALL=C sed ...` approach in comments on _all_ answers on this page, including the currently accepted one. For the complete picture, I suggest consulting my [own answer](http://stackoverflow.com/a/23584470/45375) on the duplicate question. – mklement0 Sep 04 '16 at 03:48
19

I managed to do it by running:

unset LANG

before the sed command.

Not sure what I've done or why it works but it did.

Adam Waite
  • 19,175
  • 22
  • 126
  • 148
  • I needed to do the above fix when following this tutorial: http://projectpoppycock.com/angularjs-phonegap-and-angular-seed-lets-go/ – bulltorious Feb 21 '14 at 15:57
  • This removes the error, but actually solves nothing for me. – Harrison Powers Apr 24 '14 at 23:00
  • 1
    As for why it works: If your locale was initially _set_ with `[export] LANG=...` (as opposed to setting `LC_ALL` or setting `LC_*` categories individually), then unsetting `LANG` makes all `LC_*` categories revert to `"C"`; ending up with a `LC_CTYPE` value of `C` is what solves the problem. Conversely, if `LC_ALL` or even `LC_CTYPE` specifically are set to something other than `C`, your approach won't work. – mklement0 May 11 '14 at 14:14
  • @mklement0, I think your comments make a nice answer...! – Arjan Jan 12 '15 at 14:56
  • 1
    @Arjan: Thanks; I actually did create an answer - not to this, but a very similar question: http://stackoverflow.com/a/23584470/45375 – mklement0 Jan 12 '15 at 16:38
  • `(unset LANG; locale)` <--- LC_CTYPE defaults to C –  Sep 16 '17 at 18:14