我发现这个命令可以将背景颜色改为背面:
echo -ne '\e]11;#000000\e\\'
那么,我该如何再次更改默认值呢?
谢谢 :3
答案1
答案2
这个想法是将 #000000 替换为你选择的颜色。如果“默认”是白色,你可以运行
echo -ne '\e]11;#ffffff\e\\'
现在,由于 OP 希望将其放在 bash 脚本中,因此我改编了 blueyed 的脚本,可在此处获取https://gist.github.com/blueyed/c8470c2aad3381c33ea3
#!/bin/sh
# Query a property from the terminal, e.g. background color.
#
# XTerm Operating System Commands
# "ESC ] Ps;Pt ST"
#
# Source:
# Adapted from https://gist.github.com/blueyed/c8470c2aad3381c33ea3
# Posted at:
# https://stackoverflow.com/questions/2507337/how-to-determine-a-terminals-background-color#7767891
#
# save stty
oldstty=$(stty -g)
# Such that no input is needed for the read command:
stty raw -echo min 0 time 0
#11 for background color
printf "\033]11;?\033\\"
# xterm needs the sleep
sleep 0.1
read -r answer
# Change background color for #fff000 (as an example)
printf "\033]11;#fff000\033\\"
# Do your stuff on yellow background
# Here for purpose of demonstration just sleep
sleep 1
#
#
#
# Restore background-color.
printf "\033$answer\\"
# Restore stty
stty $oldstty
然后,您可以将实际代码放入此脚本中并修改自定义背景颜色(此处为黄色)和睡眠时间等。我仅在 gnome-terminal 上进行了测试。
答案3
如果您的终端支持通过 OSC 104/110/111 重置颜色:
echo -ne '\e]111\e\\'