因為讓字串有顏色也是需要字符的啊~
沒猜錯應該是
control_node=$'\033'$'['0$';'33m''https://192.168.49.2:8443''$'\033'$'['0m
K8S_SERVER='https://192.168.49.2:8443'
echo $control_node
echo $K8S_SERVER
算字符數:
echo $control_node | wc -c # 37
echo $K8S_SERVER | wc -c # 26
你可以把黃色那個這樣測試
echo $control_node | cat -e # ^[[0;33mhttps://192.168.49.2:8443^[[0m$
echo $K8S_SERVER | cat -e # https://192.168.49.2:8443$
或者用 Python 看
echo $control_node | python -c 'print(repr(input()))'
# '\x1b[0;33mhttps://192.168.49.2:8443\x1b[0m'
echo $K8S_SERVER | python -c 'print(repr(input()))'
# 'https://192.168.49.2:8443'
#!/usr/bin/env python
# 注意這邊是 Python code!
control_node = '\x1b[0;33mhttps://192.168.49.2:8443\x1b[0m'
K8S_SERVER = 'https://192.168.49.2:8443'
print(control_node)
print(K8S_SERVER)
print(len(control_node)) # 36 # 因為差一個 echo 加的 '\n'
print(len(K8S_SERVER)) # 25