shell 生成随机字母数字组合的问题


之前在社区里看到了一个类似的帖子 这个脚本是如何产生随机密码的?
自己想到了一个其他的实现方式:

   
  head -c 500 /dev/urandom | sed 's/[^a-zA-Z0-9]//g' | head -c 16; echo
 

输出结果:

   
  ▒5▒F▒0g7K▒▒v▒7▒
 

结果并不是我想要的,看起来像是sed的替换出了问题,但问题出在哪里呢?还请大家帮忙解个惑

sed shell

中国铁路总公司 13 years, 6 months ago

需要在调用 sed 前传入环境变量 LANG=C ,否则 [a-zA-Z0-9] 表示的并不仅仅是字母数字。

   
  head -c 500 /dev/urandom |
  
LANG=C sed 's/[^a-zA-Z0-9]//g' |
head -c 16

fnashu answered 13 years, 6 months ago

Your Answer