1

For example,

# Execute the pre-hook.
export SHELL=@shell@
param1=@param1@
param2=@param2@
param3=@param3@
param4=@param4@
param5=@param5@
if test -n "@preHook@"; then
    . @preHook@
fi

For context, this is from a shell script in a commit from 2004 in the Nixpkgs repo; tried to see if this maybe a reference feature but string "shell" only occurs once (in a case-sensitive search) in the entire file.

toraritte
  • 6,300
  • 3
  • 46
  • 67
  • 1
    Comments moved to an answer, thank you for the prodding to do so. – Charles Duffy May 01 '21 at 18:35
  • @CharlesDuffy Appreciate your time for imparting these details! – toraritte May 01 '21 at 18:39
  • 1
    BTW, there are some other `@foo@` examples out there that aren't shell-related at all; if my memory serves (it may not, it's been a lot of years) that's the same form CVS used to substitute things like last-change timestamps and revision numbers into source files that needed them, f/e. – Charles Duffy May 01 '21 at 18:43
  • 1
    (Looked it up, and I was wrong; that was `$foo$`) – Charles Duffy May 01 '21 at 18:45

2 Answers2

3

The @ symbol has no meaning to the shell -- it is a punctuation character that will pretty much never occur in any actual shell script.

This makes it a good choice to use for patterns in script templates -- the basic idea being that a simple search-and-replace process will be used (perhaps with a sed script as in the link you show) to rewrite the template into an actual shell script. Every string of the form @name@ in the template will be replaced by some other string related to the environment in which the script is being installed.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
3

The answer by Chris Dodd is correct, insofar as there's no intrinsic meaning to the shell -- and @foo@ is thus commonly used as a sigil. Insofar as you encountered this in nixpkgs, it provides some stdenv tools specifically for implementing this pattern.

As documented at https://nixos.org/manual/nixpkgs/stable/#ssec-stdenv-functions, nixpkgs stdenv provides shell functions including substitute, substituteAll, substituteInPlace &c. which will replace @foo@ values with the content of corresponding variables.


In the context of the linked commit, subsitutions of that form can be seen being performed in pkgs/build-wrapper/gcc-wrapper/builder.sh:

    sed \
        -e "s^@gcc@^$src^g" \
        -e "s^@out@^$out^g" \
        -e "s^@bash@^$SHELL^g" \
        -e "s^@shell@^$shell^g" \
        < $gccWrapper > $dst

...is replacing @out@ with the value of $out, @bash@ with the value of $SHELL, etc.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 1
    "as documented" ... see also [substituteAll in the stdenv chapter](https://github.com/NixOS/nixpkgs/blob/master/doc/stdenv/stdenv.chapter.md#substituteall-infile-outfile-fun-substituteall). see also [makeSetupHook in trivial-builders.nix](https://github.com/NixOS/nixpkgs/blob/master/pkgs/build-support/trivial-builders.nix) – milahu Jul 15 '22 at 16:05