Environment Variables – Significance of $(logname)

environment-variables

sudo sh -c 'echo "$(logname) ALL=(ALL:ALL) NOPASSWD: ALL" > /etc/sudoers.d/$(logname)' & sudo chmod 440 /etc/sudoers.d/$(logname)

I use the above one-liner to allow the current user to execute sudo without a password, on remotely connected hosts.

But what exactly is $(logname)?

On a local machine, echo $(logname) returns nothing, while echo $LOGNAME does, so it's not the environmental variable, although I have never seen the value to be any different on remote machines.

Also, are there any other similar $(foo) variables (or whatever they are called)?

Best Answer

logname(1) is a command that will return the login name of the current user.
$( ... ) is the syntax for command substitution which is saying "substitute the output of the command here"

So if your user is foo you are executing:

sudo sh -c 'echo "foo ALL=(ALL:ALL) NOPASSWD: ALL" > /etc/sudoers.d/foo' & sudo chmod 440 /etc/sudoers.d/foo