I can’t seem to find the difference between a script run two different ways.
Here’s the script (named test.sh):
#! /bin/bash
printf "%b\n" "\u5A"
When the script is sourced:
. test.sh
> Z ## Result I want ##
When the script is run:
./test.sh
> \u5A ## Result I get ##
I want the run script to give the results of the sourced script… what setting do I need to set/change?
HAHA!!! I finally traced down the problem! Read ahead if interested (leave the page if not).
These are the only command that will translate
\uproperly:All of the following produce identical results in that they do NOT translate
\u:The only way to get proper translation is to run the script without a hash-bang… and I finally figured out why! Without a hash-bang my system chooses the default shell, which btw is NOT
/bin/bash… it turns out to be/opt/local/bin/bash… two different versions of bash!Finally, I removed the OSX
/bin/bash[v3.2.48(1)] and replaced it with the MacPorts/opt/local/bin/bash[v4.2.10(2)] and now running the script works! It actually solved about 10-15 other problems I’ve had (like${var,,},read sN1 char,complete -EC "echo ' '", and a host of other commands I have scattered throughout my scripts,~/.bashrcamd~/.profile). Honestly, I really should have noticed when my scripts using associative arrays suddenly crapped out on me… how stupid can I get!?I’ve been using bash v4 for a looong time now, and my Lion upgrade went and down-graded bash back to v3 (get with the program Apple!)… ugh, I feel so ashamed! Everyone still using bash v3, upgrade!! bash v4 is has many, many beautiful upgrades over version 3. Type
bash --versionto see what version you are running. One advantage is now bash can translate\uHEXinto Unicode!