-1

I just found a great answer to generate random valid hex code colors here:

var randomColor = "#000000".replace(/0/g,function(){return (~~(Math.random()*16)).toString(16);});

and I would like to translate that to bash, I'm still scratching my head to do so. I appreciate if you can shed some lights to achieve this.

Any help is much appreciated, thanks in advance

Community
  • 1
  • 1
Gery
  • 8,390
  • 3
  • 22
  • 39
  • it's just a simple question, but no problem, I will delete it, thanks anyway – Gery Jul 30 '15 at 15:29
  • I just undeleted it because of Jaime, thanks dude!! – Gery Jul 30 '15 at 15:35
  • Btw, you can downvote or close my post if you want it and feel it @Frederic and the other 4 guys, I just don't care about votes, it's about learning IMHO =) – Gery Jul 30 '15 at 15:40
  • You may have a hard time learning since the answer does not contain any explanation whatsoever, but if that suits you, fine. However, I find quite odd that the automatically generated comment with my custom close reason was removed -- it was not rude or offensive, and moderators are not supposed to remove close reason comments. Bah, it's not like I will lose sleep over this anyway. – Frédéric Hamidi Jul 30 '15 at 15:52
  • I didn't delete that if you ask me, so good sleep 8-), and Jaime's answer is pretty easy to understand, so he provided a constructive idea and solution, so he's part of the solution. – Gery Jul 30 '15 at 15:54

1 Answers1

2

cat /dev/urandom | tr -dc 'a-f0-9' | fold -w 6 | head -n 1

Jamie
  • 2,181
  • 1
  • 21
  • 35
  • @Jaime thanks for that useful answer, just one question, is there a way to get unique values? I mean, with `for i in `seq 1 1000`; do cat /dev/urandom | tr -dc 'a-f0-9' | fold -w 6 | head -n 1; done` I get 1000 different codes?, thanks again! – Gery Jul 30 '15 at 15:38
  • simply with `cat uniques | sort | uniq` just found they are "uniq", so it really works, thanks again @Jaime! – Gery Jul 30 '15 at 15:50
  • you can use `head -c6` insteed of `fold -w 6 | head -n 1` . – Yunus Jul 22 '17 at 12:03