0

Now I have a JSON string in bash:

str='{"output_name":"http://HOST:33445/data/results/merged_video.mp4"}'

And I got an ip address from a command:

HOST_IP_FOR_FUNC="$(minikube ssh "route -n | grep ^0.0.0.0 | awk '{ print \$2 }'")" && echo $HOST_IP_FOR_FUNC

The result is 192.168.64.1.

I'd like to use this variable to replace "HOST" in str.

So I used:

z=${str//HOST/$HOST_IP_FOR_FUNC} && echo $z

only got

:33445/data/results/merged_video.mp4"}

However, if I explicitly assign a value to the HOST_IP_FOR_FUNC:

ip="192.168.64.1"
z=${str//HOST/$ip} && echo $z

Then I got what I want:

{"output_name":"http://192.168.64.1:33445/data/results/merged_video.mp4"}

How can I deal with it?

I am using zsh or bash on MacOS 10.15.3

According to @Cyrus, I got the output of his suggested command:

minikube ssh "route -n | grep ^0.0.0.0 | awk '{ print \$2 }'" | hexdump -C

00000000  31 39 32 2e 31 36 38 2e  36 34 2e 31 0d 0a        |192.168.64.1..|
0000000e
  • It seems that you have some unescaped " symbols in the minikube command. Your `HOST_IP_FOR_FUNC` does not get properly populated. Try to escape i.e. use `\"` for the second and third " in the command. – jpe Feb 29 '20 at 03:57
  • Add output of `minikube 'ssh route -n' | grep ^0.0.0.0 | awk '{ print $2 }' | hexdump -C` to your question (no comment). – Cyrus Feb 29 '20 at 04:03

2 Answers2

0

Just glancing at your variable I can tell you it's most likely due to your quotes. You need to escape your inner double quotes. Try this HOST_IP_FOR_FUNC="$(minikube ssh \x22route -n | grep ^0.0.0.0 | awk '{ print \$2 }'\x22)" && echo $HOST_IP_FOR_FUNC \x22 being the hex for double quotes. See this post for more details How to escape a double quote inside double quotes?

bgibers
  • 200
  • 10
0

First remove hex 0d (carriage return) from string HOST_IP_FOR_FUNC:

HOST_IP_FOR_FUNC="${HOST_IP_FOR_FUNC/$'\x0d'/}"
Cyrus
  • 84,225
  • 14
  • 89
  • 153