entropitor/dotenv-cli

Variable expansion in cli not working

Closed this issue · 6 comments

I want to use a variable in command line, something like echo $MY_VAR

i tried
dotenv echo "$MY_VAR" // ECHO is on
dotenv -- echo "$MY_VAR" //ECHO is on

i also tried this as the documentation

{
"scripts": {
"_print-stuff": "echo $STUFF",
"print-stuff": "dotenv -- npm run _print-stuff",
}
}

then
npm run print-stuff // $STUFF

Please read the readme: https://github.com/entropitor/dotenv-cli?tab=readme-ov-file#variable-expansion-in-the-command

and look for other issues like e.g. if you searched for expansion you would have found #115, #41 and there is even also #31

thanks for the fast reply, but i did read the documentation and the mentioned example didnt work for me, as i mentioned,

using the Subscript encapsulation example, running npm run print-stuff only prints the string $STUFF

using npx dotenv -- bash -c 'echo "$STUFF"' also didnt work, it prints an empty string

using dotenv -- bash -c 'echo "$STUFF"' only works in bash not windows terminal

Can you provide more information about your environment and what your .env file looks like? Are you on windows?

using dotenv -- bash -c 'echo "$STUFF"' only works in bash not windows terminal

If that works, it feels like it has more to do with windows terminal etc. It might be that you need to figure out how the windows terminal works...

It can help to put echo before your command, e.g. echo dotenv -- bash -c 'echo "$STUFF"' to see what is printed, if $STUFF is gone, dotenv-cli never receives it and so nothing we can do about it...

I am on windows, i use windows terminal with Powershell, my .env STUFF=stuff
using echo dotenv -- bash -c 'echo "$STUFF"' the output is
dotenv
bash
-c
echo "$STUFF"

using cross-var worked, based on this article

I think its an environment issue,
bash uses $VAR
powershell uses $env:VAR or $VAR depending where you are reading from
cmd uses %VAR%

so eventually what worked for me is this

{
   "scripts": {
      "_print-stuff": "echo %STUFF%",
      "print-stuff": "dotenv -- npm run _print-stuff",
   }
}