Dealing with shell variables in nix strings

I have something like this in a systemd service

script = ''
  $(for x in `${pkgs.coreutils}/bin/ls ${directory}`; do echo ${x}; done)
'';

Is there a way to escape ${x} so it is part of the shell script and ignored by nix?

Yes, you can escape ${x} by prefixing it with 2 single-quotes: ''${x}, though remember, that there is no need to escape $x.

Similarily, if you ever need 2 singlequotes, just write 3 of them.

5 Likes

$ can be escaped by '' in the context of '', see the Nix manual.

1 Like

Thanks to both of you. :laughing: