aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • Engineering
  • Technology

Make A Web-Safe Color Guide With Bash

  • aster.cloud
  • April 13, 2023
  • 4 minute read

Use the for loop in Bash to create a handy color palette for the web.

When computer displays had a limited color palette, web designers often used a set of web-safe colors to create websites. While modern websites displaying on newer devices can display many more colors than the original web-safe color palette, I sometimes like to refer to the web-safe colors when I create web pages. This way I know my pages look good anywhere.


Partner with aster.cloud
for your next big idea.
Let us know here.



From our partners:

CITI.IO :: Business. Institutions. Society. Global Political Economy.
CYBERPOGO.COM :: For the Arts, Sciences, and Technology.
DADAHACKS.COM :: Parenting For The Rest Of Us.
ZEDISTA.COM :: Entertainment. Sports. Culture. Escape.
TAKUMAKU.COM :: For The Hearth And Home.
ASTER.CLOUD :: From The Cloud And Beyond.
LIWAIWAI.COM :: Intelligence, Inside and Outside.
GLOBALCLOUDPLATFORMS.COM :: For The World's Computing Needs.
FIREGULAMAN.COM :: For The Fire In The Belly Of The Coder.
ASTERCASTER.COM :: Supra Astra. Beyond The Stars.
BARTDAY.COM :: Prosperity For Everyone.

You can find web-safe color palettes on the web, but I wanted to have my own copy for easy reference. And you can make one too, using the for loop in Bash.

Bash for loop

The syntax of a for loop in Bash looks like this:

for variable in set ; do statements ; done

As an example, say you want to print all numbers from 1 to 3. You can write a quick for loop on the Bash command line to do that for you:

$ for n in 1 2 3 ; do echo $n ; done
1
2
3

The semicolons are a standard Bash statement separator. They let you write multiple commands on a single line. If you were to include this for loop in a Bash script file, you might instead replace the semicolons with line breaks and write out the for loop like this:

for n in 1 2 3
  do
    echo $n
  done

I like to include the do on the same line as the for so it’s easier for me to read:

for n in 1 2 3 ; do
  echo $n
done

More than one for loop at a time

You can put one loop inside another. That can help you to iterate over several variables, to do more than one thing at a time. Let’s say you wanted to print out all combinations of the letters A, B, and C with the numbers 1, 2, and 3. You can do that with two for loops in Bash, like this:

#!/bin/bash
for number in 1 2 3 ; do
  for letter in A B C ; do
    echo $letter$number
  done
done

If you put these lines in a Bash script file called for.bash and run it, you see nine lines showing the combinations of all the letters paired with each of the numbers:

$ bash for.bash
A1
B1
C1
A2
B2
C2
A3
B3
C3

Looping through the web-safe colors

The web-safe colors are all colors from hexadecimal color #000 (black, where the red, green, and blue values are all zero) to #fff (white, where the red, green, and blue colors are all at their full intensities), stepping through each hexadecimal value as 0, 3, 6, 9, c, and f.

Read More  Mid-Year Update On 2022 CNCF, Linux Foundation, And Open Source Velocity

You can generate a list of all combinations of the web-safe colors using three for loops in Bash, where the loops iterate over the red, green, and blue values.

#!/bin/bash
for r in 0 3 6 9 c f ; do
  for g in 0 3 6 9 c f ; do
    for b in 0 3 6 9 c f ; do
      echo "#$r$g$b"
    done
  done
done

If you save this in a new Bash script called websafe.bash and run it, you see an iteration of all the web safe colors as hexadecimal values:

$ bash websafe.bash | head
#000
#003
#006
#009
#00c
#00f
#030
#033
#036
#039

To make an HTML page that you can use as a reference for web-safe colors, you need to make each entry a separate HTML element. Put each color in a <div> element, and set the background to the web-safe color. To make the hexadecimal value easier to read, put it inside a separate <code> element. Update the Bash script to look like this:

#!/bin/bash
for r in 0 3 6 9 c f ; do
  for g in 0 3 6 9 c f ; do
    for b in 0 3 6 9 c f ; do
      echo "&lt;div style='background-color:#$r$g$b'&gt;&lt;code&gt;#$r$g$b&lt;/code&gt;&lt;/div&gt;"
    done
  done
done

When you run the new Bash script and save the results to an HTML file, you can view the output in a web browser to all the web-safe colors:

$ bash websafe.bash &gt; websafe.html
(Jim Hall, CC BY-SA 4.0)

The web page isn’t very nice to look at. The black text on a dark background is impossible to read. I like to apply some HTML styling to ensure the hexadecimal values are displayed with white text on a black background inside the color rectangle. To make the page look really nice, I also use HTML grid styles to arrange the boxes with six per row and some space between each box.

To add this extra styling, you need to include the other HTML elements before and after the for loops. The HTML code at the top defines the styles and the HTML code at the bottom closes all the open HTML tags:

#!/bin/bash

cat&lt;&lt;EOF
&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
    &lt;title&gt;Web-safe colors&lt;/title&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;

&lt;style&gt;
div {
  padding-bottom: 1em;
}

code {
  background-color: black;
  color: white;
}

@media only screen and (min-width:600px) {

body {
  display: grid;
  grid-template-columns: repeat(6,1fr);
  column-gap: 1em;
  row-gap: 1em;
}

div {
  padding-bottom: 3em;
}

}
  &lt;/style&gt;
&lt;/head&gt;
&lt;/body&gt;
EOF

for r in 0 3 6 9 c f ; do
for g in 0 3 6 9 c f ; do
for b in 0 3 6 9 c f ; do

echo "&lt;div
style='background-color:#$r$g$b'&gt;&lt;code&gt;#$r$g$b&lt;/code&gt;&lt;/div&gt;"

done
done
done

cat&lt;&lt;EOF

&lt;/body&gt;
&lt;/html&gt;
EOF

This finished Bash script generates a web-safe color guide in HTML. Whenever you need to refer to the web-safe colors, run the script and save the results to an HTML page. Now you can see a representation of the web-safe colors in your browser as an easy-reference guide for your next web project:

$ bash websafe.bash &gt; websafe.html
(Jim Hall, CC BY-SA 4.0)

By: Jim Hall
Originally published at Opensource


For enquiries, product placements, sponsorships, and collaborations, connect with us at [email protected]. We'd love to hear from you!

Our humans need coffee too! Your support is highly appreciated, thank you!

aster.cloud

Related Topics
  • Bash
  • Linux
  • Scripting
  • Tutorials
  • Web Development
You May Also Like
View Post
  • Engineering

Just make it scale: An Aurora DSQL story

  • May 29, 2025
View Post
  • Computing
  • Multi-Cloud
  • Technology

Reliance on US tech providers is making IT leaders skittish

  • May 28, 2025
View Post
  • Computing
  • Multi-Cloud
  • Technology

TD Synnex named as UK distributor for Cohesity

  • May 28, 2025
View Post
  • Computing
  • Multi-Cloud
  • Technology

Broadcom’s ‘harsh’ VMware contracts are costing customers up to 1,500% more

  • May 28, 2025
View Post
  • Computing
  • Multi-Cloud
  • Technology

Pulsant targets partner diversity with new IaaS solution

  • May 23, 2025
View Post
  • Computing
  • Multi-Cloud
  • Technology

Growing AI workloads are causing hybrid cloud headaches

  • May 23, 2025
Gemma 3n
View Post
  • Technology

Announcing Gemma 3n preview: powerful, efficient, mobile-first AI

  • May 22, 2025
View Post
  • Computing
  • Multi-Cloud
  • Technology

Google is getting serious on cloud sovereignty

  • May 22, 2025

Stay Connected!
LATEST
  • 1
    Just make it scale: An Aurora DSQL story
    • May 29, 2025
  • 2
    Reliance on US tech providers is making IT leaders skittish
    • May 28, 2025
  • 3
    TD Synnex named as UK distributor for Cohesity
    • May 28, 2025
  • 4
    Broadcom’s ‘harsh’ VMware contracts are costing customers up to 1,500% more
    • May 28, 2025
  • 5
    Pulsant targets partner diversity with new IaaS solution
    • May 23, 2025
  • 6
    Growing AI workloads are causing hybrid cloud headaches
    • May 23, 2025
  • Gemma 3n 7
    Announcing Gemma 3n preview: powerful, efficient, mobile-first AI
    • May 22, 2025
  • 8
    Google is getting serious on cloud sovereignty
    • May 22, 2025
  • oracle-ibm 9
    Google Cloud and Philips Collaborate to Drive Consumer Marketing Innovation and Transform Digital Asset Management with AI
    • May 20, 2025
  • 10
    Hybrid cloud is complicated – Red Hat’s new AI assistant wants to solve that
    • May 20, 2025
about
Hello World!

We are aster.cloud. We’re created by programmers for programmers.

Our site aims to provide guides, programming tips, reviews, and interesting materials for tech people and those who want to learn in general.

We would like to hear from you.

If you have any feedback, enquiries, or sponsorship request, kindly reach out to us at:

[email protected]
Most Popular
  • 1
    Cloud adoption isn’t all it’s cut out to be as enterprises report growing dissatisfaction
    • May 15, 2025
  • notta-ai-header 2
    Notta vs Fireflies: Which AI Transcription Tool Deserves Your Attention in 2025?
    • May 16, 2025
  • oracle-ibm 3
    IBM and Oracle Expand Partnership to Advance Agentic AI and Hybrid Cloud
    • May 6, 2025
  • college-of-cardinals-2025 4
    The Definitive Who’s Who of the 2025 Papal Conclave
    • May 7, 2025
  • conclave-poster-black-smoke 5
    The World Is Revalidating Itself
    • May 6, 2025
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.