cade galt
cade galt

Reputation: 4181

What does $() do?

Trying to troubleshoot this issue:

https://unix.stackexchange.com/questions/128894/ssh-exchange-identification-connection-closed-by-remote-host-not-using-hosts-d

and part of one solution is to use:

$(which sshd)

which in my case outputs:

Could not load host key: /etc/ssh_host_rsa_key
Could not load host key: /etc/ssh_host_dsa_key

I keep a cheat sheet for all my bash commands and wanted to add in:

$()

It appears to be doing something to the sshd executable.

Related

What does it mean in shell when we put a command inside dollar sign and parentheses: $(command)

Upvotes: 2

Views: 199

Answers (2)

Oliver Gondža
Oliver Gondža

Reputation: 3510

$() is a way to execute another process and collect its output. See http://wiki.bash-hackers.org/syntax/expansion/cmdsubst for more details.

When such expression is passed to bash, its output gets executed. It effectively call the command using its full path, as that is what which returns. The messages printed are from sshd process started by that expression.

Note that which locates executable scanning $PATH, same as when you execute the command. In other words, executing which output it is not going to affect which executable is run, only the full path to executable tracked by operating system.

Upvotes: 3

Jeff Bowman
Jeff Bowman

Reputation: 95754

$(which sshd) will be replaced by the stdout resulting from running which sshd. which sshd will return the fully-qualified path of the executable invoked when invoking sshd:

which returns the pathnames of the files (or links) which would be executed in the current environment, had its arguments been given as commands in a strictly POSIX-conformant shell. It does this by searching the PATH for executable files matching the names of the arguments. It does not follow symbolic links.

Examples, as run on a command line, where > represents the input prompt:

COMMAND: which sshd
 OUTPUT: /usr/sbin/sshd

COMMAND: echo "The full path of sshd is $(which sshd)"
 OUTPUT: The full path of sshd is /usr/sbin/sshd

COMMAND: $(which sshd)
 OUTPUT: [[whatever output you get from running /usr/sbin/sshd]]

Upvotes: 0

Related Questions