5

On the command line, I get this:

$ FOO=foo
$ echo $FOO
foo
$ echo $$FOO
11971FOO

Here, $$ resolves to the PID of the shell as expected and "FOO" is printed verbatim.

Now, trying to understand and debug some scripts, I find the following:

#!/bin/bash
FILE1=/path/to/file/1
FILE2=/path/to/file/2
echo $$FILE1 >> $$FILE2

The script in question originates from a postinstall script of a Debian package. Is this supposed to undergo pre-processing before it can run?

Update: The script is part of a package built with epm and read via the following directive:

%postinstall <script.sh

In the resulting deb package, the postinst script reads:

#!/bin/bash
FILE1=/path/to/file/1
FILE2=/path/to/file/2
echo $FILE1 >> $FILE2

Thus, the processing is done by either epm or dpkg.

Rubén
  • 34,714
  • 9
  • 70
  • 166
Jan
  • 723
  • 1
  • 11
  • 24
  • To me, this looks like a way to create a directory whose name does not collide with any existing one. – fedorqui Dec 22 '15 at 13:25
  • 1
    If that's an actual script, then it almost certainly is meant to be post-processed. It's impossible to say for sure, though, without more context. – chepner Dec 22 '15 at 13:33
  • To get this "double dereferencing" to work in bash, you'd use [variable indirection](https://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion): `foo=bar; FOO=foo; echo "${!FOO}"` or, in bash v4, use a "name reference": `foo=bar; declare -n FOO=foo; echo "$FOO"` – glenn jackman Dec 22 '15 at 15:27

2 Answers2

2

This is apparently a feature of the EPM packaging tool. Quoting the documentation:

Note that all commands specified in the list file will use the variable expansion provided by EPM, so be sure to quote any dollar sign ($) characters in your commands. For example, "$foo" is replaced by the value of "foo", but "$$foo" becomes "$foo".

tripleee
  • 175,061
  • 34
  • 275
  • 318
0

$$ prints the process ID which is 11971 in your case.

Since you have a FOO after that echo just dumps FOO after 11971 so you get

11971FOO

To be precise it is the process id of the bash shell in which you run the session. To verify this you could do :

kill -9 11971

which will terminate the current session. The below script in normal cases

#!/bin/bash
FILE1=/path/to/file/1
FILE2=/path/to/file/2
echo $$FILE1 >> $$FILE2

will not undergo any preprocessing. It will keep on appending the file

current_shell_process_id_FILE2

in the current directory with the content

current_shell_process_idFILE1
sjsam
  • 21,411
  • 5
  • 55
  • 102