Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I print the result of a command with 'echo'?

Tags:

bash

echo

How do print my full name with the following?

awk -F: '($1==U){print $5}' U=$LOGNAME /etc/passwd

Per example, but with the echo command with some words on sides:

For example,

Hello Diogo Saraiva, happy to see you again

Where Diogo Saraiva is my full name which I have in Ubuntu records. I tried some things, but I have not accomplished that script...

Another thing: Why, when I issue awk -F: '($1==U){print $5}' U=$LOGNAME /etc/passwd, is Diogo Saraiva,,, shown instead of Diogo Saraiva?

This happens too with:

grep $USER /etc/passwd | awk 'BEGIN { FS=":" } { print $5 }'

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 image 370
DiogoSaraiva Avatar asked Apr 24 '15 18:04

DiogoSaraiva


People also ask

How do you echo output of command?

The echo command writes text to standard output (stdout). The syntax of using the echo command is pretty straightforward: echo [OPTIONS] STRING... Some common usages of the echo command are piping shell variable to other commands, writing text to stdout in a shell script, and redirecting text to a file.

What does the echo command do in command line?

In computing, echo is a command that outputs the strings that are passed to it as arguments. It is a command available in various operating system shells and typically used in shell scripts and batch files to output status text to the screen or a computer file, or as a source part of a pipeline.

What does echo $? Do in Linux?

echo command in linux is used to display line of text/string that are passed as an argument . This is a built in command that is mostly used in shell scripts and batch files to output status text to the screen or a file.


1 Answers

To output the string you want you can use this command.

awk -F: -v U="$LOGNAME" '$1==U{print "Hello " $5 ", 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.

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"
like image 97
Etan Reisner Avatar answered Sep 28 '22 16:09

Etan Reisner