grizzthedj
grizzthedj

Reputation: 7505

Why is the echo command returning this kind of result?

I made a typo today echoing an environment variable, and the result was unexpected. The environment variable contains a simple path.

$ export TEST_ENV_VAR=/path/to/some/project
$ echo $TEST_ENV_VAR
/path/to/some/project

My typo was two $$ instead instead of one. I would have expected echo to return something like $/path/to/some/project in this case.

$ echo $$TEST_ENV_VAR
11513TEST_ENV_VAR

Why does echo return this type of result?

Upvotes: 0

Views: 65

Answers (2)

grizzthedj
grizzthedj

Reputation: 7505

It seems that $$ returns the pid of the current process.

So the output displayed is the pid with TEST_ENV_VAR appended to it.

Upvotes: 2

vsergi
vsergi

Reputation: 785

$$ is considered a special character.

($$) Expands to the process ID of the shell. In a () subshell, it expands to the process ID of the invoking shell, not the subshell.

As you noticed, it returns a PID. This PID is the current shell you are using. If you use the command ps aux | grep $$ you would see something like this:

1997 1 1997 19804 cons0 3293653 14:15:20 /usr/bin/bash

Which means that in my case, I am using bash as shell.

Source

Upvotes: 2

Related Questions