Hyphen is being replaced by $'\342\200\224...' in bash
I've created a bash script to start a network computer if it is sleeping, then start a streaming client. However, as the script executes the final command to run the streaming client, each cli option with a hyphen becomes wrapped in $'\342\200\224<option>'
. E.g. -resolution
becomes $'\342\200\224resolution'
.
Specifically, this command in the script:
/Applications/Moonlight.app/Contents/MacOS/Moonlight stream "$_target_computer_ip" "$_moonlight_app_name" —resolution "$_moonlight_resolution" —fps "$_moonlight_fps" &>/dev/null &
executes in the shell as:
/Applications/Moonlight.app/Contents/MacOS/Moonlight stream 192.168.1.30 mstsc.exe $'\342\200\224resolution' 1920x1024 $'\342\200\224fps' 30
I've tried escaping the hyphen as -, but that just changes the output to $'?\200\224
. What am I doing wrong?
Here's the full script:
#!/bin/bash
set -x
_target_computer_ip='192.168.1.30'
_target_computer_subnet='192.168.1.255'
_target_computer_mac='2C:F0:5D:27:7C:95'
_moonlight_app_name='mstsc.exe'
_moonlight_resolution='1920x1080'
_moonlight_fps='30'
if ping -c 1 -W 1 "$_target_computer_ip"; then
echo "is alive"
else
echo "nope"
wakeonlan -i $_target_computer_subnet $_target_computer_mac
fi
/Applications/Moonlight.app/Contents/MacOS/Moonlight stream "$_target_computer_ip" "$_moonlight_app_name" —resolution "$_moonlight_resolution" —fps "$_moonlight_fps" &>/dev/null &
And the full command output:
Fayes-MBP:Shell Scripts rain$ ./check_and_wake_pc.sh
+ _target_computer_ip=192.168.1.30
+ _target_computer_subnet=192.168.1.255
+ _target_computer_mac=2C:F0:5D:27:7C:95
+ _moonlight_app_name=mstsc.exe
+ _moonlight_resolution=1920x1080
+ _moonlight_fps=30
+ echo 192.168.1.30
192.168.1.30
+ ping -c 1 -W 1 192.168.1.30
PING 192.168.1.30 (192.168.1.30): 56 data bytes
--- 192.168.1.30 ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss, 1 packets out of wait time
round-trip min/avg/max/stddev = 3.701/3.701/3.701/0.000 ms
+ echo 'is alive'
is alive
+ /Applications/Moonlight.app/Contents/MacOS/Moonlight stream 192.168.1.30 mstsc.exe $'?\200\224resolution' 1920x1080 $'?\200\224fps' 30
Solution 1:
You're using —
, which is an em-dash, instead of a hyphen (-
) in the script. With non-proportional fonts such as Terminal (or code formatting on websites) this is hard to see; in regular text, it's more obvious: — vs. -
According to this page, the UTF-8 encoding of an em-dash is 0xE2 0x80 0x94
, and when you convert that to octal you get 342 200 224
, which are the numbers you see in your output. (Somebody with more bash knowledge than me can explain why this happens.)
Hyphens often automatically get converted to other kind of dashes (on macOS, iOS as well on Windows) which is helpful for the majority of people using computers, but unfortunately not for programmers or system administrators ...