Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I make echo process color commands from variable?

Tags:

bash

echo

colors

I am writing a bash script in which I need color output. I have a text file that needs to be formatted like this when it's printed to the screen:

>Barry's Whiskey Glass Drink Cup, XL 10 oz Dri...        $0.75
example.org/product-page.php?id=1228741

>Cotton Bath Towel Pool Towel Brown - Easy Car...        $6.11  
example.org/product-page.php?id=1228763

>Cotton Bath Towel Pool Towel Red - Easy Care ...        $6.11     
example.org/product-page.php?id=1228766

>Mosiso MacBook Case for iPad Pro 12.9/MacBook...        $1.95  
example.org/product-page.php?id=1228850

The '>....." text needs to be one color, '$..." another color, and "example.org...." light grey. I have tried this:

  tac newlist.txt | sed 's/ >/\r\n   >/' > bwtext.txt

  sed -i 's/>/>\\033\[0;34m/g' bwtext.txt
  sed -i 's/\$/\$\\033\[0;33m/g' bwtext.txt
  sed -i 's/http/\\033\[0;37mhttp/g' bwtext.txt

  while read line
     do
        echo -e "${line}"
     done < bwtext.txt

That inserts the correct escape codes into the file, but when echoed out line by line, instead of processing the codes, it just prints them literally.

>033[0;34mFor Fitbit Alta Accessory Band, Silicone Repl...        $033[0;33m1.97      
033[0;37mexample.org/product-page.php?id=1247364

>033[0;34mTattify Gradient Nail Wraps - Love Like a Sun...        $033[0;33m0.99      
033[0;37mexample.org/product-page.php?id=1247367

>033[0;34mEA AromaCare Eucalyptus Essential Oil 120ml/4...        $033[0;33m3.00      
033[0;37mexample.org/product-page.php?id=1247370

... waiting 10 minutes for next update ...

What am I doing wrong, or how can I do it right? Thanks.

like image 891
rebeltaz Avatar asked Feb 24 '26 20:02

rebeltaz


1 Answers

You have correct escape sequences in file but those are not being read in your while loop. Try adding raw input flag ( -r ) while reading which will not interpret the escape sequences and give you desired formatting.

while read -r line
do
echo -e "${line}"
done < bwtext.txt
like image 78
ppp Avatar answered Feb 26 '26 10:02

ppp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!