Anthony Kong Anthony Kong - 23 days ago 9
Ruby Question

Chef complaints about invalid Unicode escape

I want to set the following content in my .bash_profile

bash_profile_content = %Q(
export EDITOR=vi
export ENV=#{role}
export PATH=$PATH:/usr/local/bin
export PS1="${debian_chroot:+($debian_chroot)}\u@\h:\w [$ENV]\$ "
)

file '/home/me/.bash_profile' do
cotent bash_profile_conent
end


Chef throws this error:

==> Server-002: [2015-10-07T03:02:23+00:00] ERROR: Exception handlers complete
==> Server-002: Chef Client failed. 0 resources updated in 322.419863736 seconds
==> Server-002: [2015-10-07T03:02:23+00:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
==> Server-002: [2015-10-07T03:02:23+00:00] ERROR: /var/chef/cache/cookbooks/servers/recipes/_common_user.rb:67: invalid Unicode escape
==> Server-002: ...ian_chroot:+($debian_chroot)}\u@\h:\w [$ENV]\$ "
==> Server-002: ...


Apparently chef thinks the
@
character is meant for unicode escape.

I want to use
@
as is. How can I fix this error?

Answer

The isn't really the problem, the \u is. For example:

irb > "\u@"
SyntaxError: (irb):1: invalid Unicode escape
"\u@"
   ^

but:

irb > "\u0611"
 => "ؑ" 

Keep in mind that %Q(...) behaves like a double quoted string so all the usual escapes (such as \u for Unicode) apply. Adding more backslashes should sort things out:

bash_profile_content = %Q(
export EDITOR=vi
export ENV=#{role}
export PATH=$PATH:/usr/local/bin
export PS1="${debian_chroot:+($debian_chroot)}\\u@\\h:\\w [$ENV]\\$ "
)
Comments