bash - Why does redirection in `< /dev/urandom tr | tr` work; how does this parse?
问题描述
I use this to make passwords:
echo $(LC_CTYPE=C < /dev/urandom tr -cd [:graph:] | tr -d '\n' | head -c32) # pwg ##1st
Varying length, graph to alnum, or whatever.
I don't understand the redirection being used. I understand the outer $() format but this
echo $(LC_CTYPE=C < /dev/urandom tr -cd [:graph:] ...
doesn't make sense.
- The LC_CTYPE is setting the character type. Got that.
tr
is converting characters to those in the set :graph:. Got that.- The echo is there to give a line-break.
- But how can you redirecct urandom in to a env variable setting expression. ?
- How, does
tr
know to retrieve urandom from before it. ?
To me it seems it should be:
echo $(LC_CTYPE=C tr -cd [:graph:] < /dev/urandom | tr -d '\n' | head -c32) # pwg ##2nd
Which also works. I just can't understand why the first variant works?
解决方案
手册中的一些信息:
在执行命令之前,它的输入和输出可能会使用由 shell 解释的特殊符号重定向... 以下重定向运算符可能出现在简单命令的任何位置之前或出现在命令之后。...
执行简单命令时,shell 从左到右执行以下扩展、赋值和重定向。
- 解析器标记为变量赋值(命令名前面的那些)和重定向的词被保存以供以后处理。
- 不是变量赋值或重定向的词被扩展(参见 Shell 扩展)。如果展开后还剩下任何单词,则将第一个单词作为命令的名称,其余单词作为参数。
- 如上所述执行重定向(请参阅重定向)。
- 在分配给变量之前,每个变量赋值中“=”之后的文本都经过波浪号扩展、参数扩展、命令替换、算术扩展和引号删除。
推荐阅读
- input - 使用不带分隔符的输入值在具体化日期选择器中预选日期
- swift - 无法使用“Double”类型的参数为“[Double]”类型的值下标
- c# - 我如何知道哪个 DependencyProperty 与特定 EffectiveValue 索引相关
- vba - ActiveDocument.SaveAs2 保存文档,但一旦保存就无法打开文档(仅限某些计算机型号)
- javascript - 如何使用捆绑包中的 node_modules 依赖项正确构建用于生产的 NestJS 应用程序?
- logging - 将 Logger 与 DASK Futures 一起使用
- android - Google 在没有任何通知的情况下删除了我的应用。这发生在其他人身上吗?
- postgresql - Postgresql:由于使用临时表的每个事务的最大锁定数而导致共享内存不足
- vpn - 尝试连接到 Google Cloud Platform 中的主机
- sql - 两张表,返回不同的许可证号