我听说printf
比echo
. 根据我的经验,我只能回忆起一个我不得不使用的实例,printf
因为它echo
无法将一些文本输入到 RHEL 5.8 上的某个程序中,但printf
确实如此。但显然,还有其他差异,我想询问它们是什么以及是否有特定情况下何时使用一种与另一种。
我显然明白可以为内部字段分隔符变量添加值。例如:
$ IFS=blah
$ echo "$IFS"
blah
$
Run Code Online (Sandbox Code Playgroud)
我也明白这read -r line
会将数据保存stdin
到名为的变量line
:
$ read -r line <<< blah
$ echo "$line"
blah
$
Run Code Online (Sandbox Code Playgroud)
但是,命令如何分配变量值?它是否首先存储来自stdin
to 变量的数据line
,然后赋予line
to 的值IFS
?
这个问题是关于将一个变量的全部内容分配给另一个变量。
未使用变量(发送至echo
等)
赋值期间没有进行参数扩展。
我的问题也仅与 POSIX shell 有关(此处未使用数组)。
error="You have an error in your input"
newVar="$error"
# vs.
newVar=$error
# ?
# ??This assignment is the question. It doesn't seem that quotes are
# needed here in order to assign the entire contents of $error to
# a new variable.
# ??(But quotes are required when they're used.)
# ?
printf "%s\n" "$error"
# => You have an error in your …
Run Code Online (Sandbox Code Playgroud) 更新为原始不清楚。
我正在计算机上设置 postfix 以使用我的电子邮件的 SMTP。
我不想以纯文本形式输入我的凭据,尤其是在公开输入时。
是否可以让 bash 不显示我对一两个命令的输入?
示例:我正在输入一系列命令,然后我想将用户名和密码回显到配置文件中。
我想快速进入我的输入不显示在屏幕上的模式,然后轻松地离开该模式ctl+c
.
bash ×2
assignment ×1
echo ×1
password ×1
posix ×1
printf ×1
quoting ×1
shell ×1
shell-script ×1