bash 如何使用“echo”打印命令的结果?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29854979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I print the result of a command with 'echo'?
提问by DiogoSaraiva
How do print my full name with the following?
如何使用以下内容打印我的全名?
awk -F: '(==U){print }' U=$LOGNAME /etc/passwd
Per example, but with the echo
command with some words on sides:
每个示例,但带有echo
一些字样的命令:
For example,
例如,
Hello Diogo Saraiva, happy to see you again
你好 Diogo Saraiva,很高兴再次见到你
Where Diogo Saraiva is my full name which I have in Ubuntu records. I tried some things, but I have not accomplished that script...
Diogo Saraiva 是我在 Ubuntu 记录中的全名。我尝试了一些东西,但我还没有完成那个脚本......
Another thing:Why, when I issue awk -F: '($1==U){print $5}' U=$LOGNAME /etc/passwd
, is Diogo Saraiva,,,
shown instead of Diogo Saraiva
?
另一件事:为什么,当我发出时awk -F: '($1==U){print $5}' U=$LOGNAME /etc/passwd
,会Diogo Saraiva,,,
显示 ,而不是Diogo Saraiva
?
This happens too with:
这也会发生在:
grep $USER /etc/passwd | awk 'BEGIN { FS=":" } { print }'
I need for my script to declare a variable, "like", with the command, and then echo that variable "like" various times along my script.
我需要让我的脚本用命令声明一个变量“like”,然后在我的脚本中多次回显该变量“like”。
回答by Etan Reisner
To output the string you want you can use this command.
要输出您想要的字符串,您可以使用此命令。
awk -F: -v U="$LOGNAME" '==U{print "Hello " ", happy to see you again"}' /etc/passwd
If awk -F: -v U="$LOGNAME" '$1==U{print $5}' /etc/passwd
is outputting Diogo Saraiva,,,
then that is what is in your /etc/passwd
file for that field.
如果awk -F: -v U="$LOGNAME" '$1==U{print $5}' /etc/passwd
正在输出,Diogo Saraiva,,,
那么这就是/etc/passwd
该字段的文件中的内容。
To save the output from a command in a variable just use:
要将命令的输出保存在变量中,只需使用:
var=$(command)
And remember to quote the variable when you use it:
并记住在使用变量时引用它:
echo "Hello $var, happy to see you again"