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  CES 2025: HP’s OMEN AI Casts a Spell on Gaming, Optimizing Performance with a Single Click

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
  • Technology

Building secure, scalable AI in the cloud with Microsoft Azure

  • July 5, 2025
View Post
  • Computing
  • Multi-Cloud
  • Technology

Turns out OpenAI is the customer behind Oracle’s mysterious $30 billion cloud deal

  • July 3, 2025
View Post
  • Computing
  • Multi-Cloud
  • Technology

A looming hyperscaler exodus? UK IT leaders are thinking of ditching US cloud providers – here’s why

  • June 26, 2025
View Post
  • Computing
  • Multi-Cloud
  • Technology

Prioritize security from the edge to the cloud

  • June 25, 2025
View Post
  • Computing
  • Multi-Cloud
  • Technology

6 edge monitoring best practices in the cloud

  • June 25, 2025
Genome
View Post
  • Technology

AlphaGenome: AI for better understanding the genome

  • June 25, 2025
View Post
  • Computing
  • Multi-Cloud
  • Technology

Pure Accelerate 2025: All the news and updates live from Las Vegas

  • June 18, 2025
View Post
  • Computing
  • Multi-Cloud
  • Technology

‘This was a very purposeful strategy’: Pure Storage unveils Enterprise Data Cloud in bid to unify data storage, management

  • June 18, 2025

Stay Connected!
LATEST
  • 1
    Building secure, scalable AI in the cloud with Microsoft Azure
    • July 5, 2025
  • 2
    Turns out OpenAI is the customer behind Oracle’s mysterious $30 billion cloud deal
    • July 3, 2025
  • aster-cloud-erp-bill_of_materials_2 3
    What is an SBOM (software bill of materials)?
    • July 2, 2025
  • aster-cloud-sms-pexels-tim-samuel-6697306 4
    Send SMS texts with Amazon’s SNS simple notification service
    • July 1, 2025
  • Camping 5
    The Summer Adventures : Camping Essentials
    • June 27, 2025
  • aster-cloud-website-pexels-goumbik-574069 6
    Host a static website on AWS with Amazon S3 and Route 53
    • June 27, 2025
  • 7
    A looming hyperscaler exodus? UK IT leaders are thinking of ditching US cloud providers – here’s why
    • June 26, 2025
  • Prioritize security from the edge to the cloud
    • June 25, 2025
  • 6 edge monitoring best practices in the cloud
    • June 25, 2025
  • Genome 10
    AlphaGenome: AI for better understanding the genome
    • June 25, 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
    There’s a ‘cloud reset’ underway, and VMware Cloud Foundation 9.0 is a chance for Broadcom to pounce on it
    • June 17, 2025
  • 2
    ‘This was a very purposeful strategy’: Pure Storage unveils Enterprise Data Cloud in bid to unify data storage, management
    • June 18, 2025
  • 3
    Pure Accelerate 2025: All the news and updates live from Las Vegas
    • June 18, 2025
  • Oracle adds xAI Grok models to OCI
    • June 17, 2025
  • What is cloud bursting?
    • June 18, 2025
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.