如何显示bash所支持的所有颜色? [英] How to show all colors supported by bash?

查看:145
本文介绍了如何显示bash所支持的所有颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一天,我输入命令

echo "\033[32mHELLOBASE\033[m"

在GNOME的bash shell。终端向我展示了一个绿色的HELLOBASH字符串。
我发现这个有趣。从我的经验和serveral的测试,我可以改变
从0到47,然后32号我写了下面code,

in the gnome bash shell. The terminal showed me a green HELLOBASH string. I found this interesting. From my experience and serveral tests, I can change the number 32 from 0 up to 47. Next I wrote the following code,

for i in {0..48};do
    echo \033[$imHELLOBASH\[033m
done

当然,这是行不通的,否则我不能在这里!因此,如何提高上述code发挥作用?

Of course, it doesn't work, or I cannot be here! So how to improve the above code to function?

推荐答案

让我们这样做的正确的方式 - 在仰视颜色codeS我们的的termcap (或,对于现代系统,的terminfo 使用 tput的命令)数据库:

Let's do this the right way -- looking up color codes in our termcap (or, for modern systems, terminfo) database using the tput command:

for ((i=0; i<=48; i++)); do
  tput setaf "$i"
  echo HELLOBASH
done

如果你想看到一个256色终端上所有可用的颜色,使用code标记从 BashFAQ#37

If you want to see all available colors on a 256-color terminal, use this code token from BashFAQ #37:

colors256() {
        local c i j

        printf "Standard 16 colors\n"
        for ((c = 0; c < 17; c++)); do
                printf "|%s%3d%s" "$(tput setaf "$c")" "$c" "$(tput sgr0)"
        done
        printf "|\n\n"

        printf "Colors 16 to 231 for 256 colors\n"
        for ((c = 16, i = j = 0; c < 232; c++, i++)); do
                printf "|"
                ((i > 5 && (i = 0, ++j))) && printf " |"
                ((j > 5 && (j = 0, 1)))   && printf "\b \n|"
                printf "%s%3d%s" "$(tput setaf "$c")" "$c" "$(tput sgr0)"
        done
        printf "|\n\n"

        printf "Greyscale 232 to 255 for 256 colors\n"
        for ((; c < 256; c++)); do
                printf "|%s%3d%s" "$(tput setaf "$c")" "$c" "$(tput sgr0)"
        done
        printf "|\n"
}
colors256

有关如何和更多的背景,为什么任何信息,查看的bash-黑客网页端codeS

For additional background on how and why any of this works, see the bash-hackers page on terminal codes.

至于为什么你原来的code没有使用ANSI颜色codeS终端甚至工作,顺便说一句 - @rici正确盯住它。你参数扩展含糊不清不增加大括号

As for why your original code didn't work even on terminals using ANSI color codes, by the way -- @rici pegged it correctly: Your parameter expansion was ambiguous without adding curly braces.

这就是说:

$imHELLOBASH

...需要是...

...needed to be...

${i}mHELLOBASH

...避免壳试图找到并展开了一个名为变量 imHELLOBASH ,而不是一个名为变量 I

...to avoid the shell trying to find and expand a variable called imHELLOBASH rather than a variable named i.

这篇关于如何显示bash所支持的所有颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆