29

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.

0

1 Answer 1

34

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"
1
  • 20
    echo $(ls /home/xx) Jun 24, 2021 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.