0

My shell script does not read .env vars on Ubuntu 24.02.

$ gnome-shell --version
GNOME Shell 46.0

This my script.

//The script
#!/bin/sh
set +x
set +e
source .env 
clear
echo "Running flash loan simulation"
echo "PRIVATE_KEY: $PRIVATE_KEY"
echo "SEPOLIA_RPC_URL $SEPOLIA_RPC_URL"
echo "line 9"
cat .env
forge clean
forge build --force

When I cat the .env file in the same directory as the script.

$ cat .env
SEPOLIA_EQUALIZER_LENDER=xxxxxxxxxxxxxxxxxxxxxxxxxx
SEPOLIA_RPC_URL=https://sepolia.infura.io/v3/xxxxxxxxxxxxxx
PRIVATE_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

The terminal output. Interestingly the cat command does not list PRIVATE_KEY here.

Running flash loan simulation
PRIVATE_KEY: 
SEPOLIA_RPC_URL 
line 9
SEPOLIA_EQUALIZER_LENDER=0x0837b2aCcF87De52a099409863CDb5f3588342AD
SEPOLIA_RPC_URL=https://sepolia.infura.io/v3/xxxxxxx
# PRIVATE_KEY= is not listed here despite being listed in the cat command above
[⠊] Compiling...4d6a4949fa623629e0ED6bd4Ecb78A8C847693
[⠢] Compiling 36 files with Solc 0.8.26
[⠆] Solc 0.8.26 finished in 1.06s
Compiler run successful!

And finally the files.

$ ls
cache  foundry.toml  lib  notes.txt  out  README.md  run.sh  script  src  test  wip.sh
$ ls -a
.  ..  cache  .env  foundry.toml  .git  .gitignore  lib  notes.txt  out  README.md  run.sh  script  src  test  wip.sh
$ ls -la
total 64
drwxrwxr-x  9 myuser myuser 4096 Oct  7 11:13 .
drwxrwxr-x  7 myuser myuser 4096 Oct  4 13:39 ..
drwxrwxr-x  2 myuser myuser 4096 Oct  7 11:13 cache
-rw-------  1 myuser myuser  195 Oct  6 16:37 .env
-rw-rw-r--  1 myuser myuser  377 Oct  6 13:15 foundry.toml
drwxrwxr-x  8 myuser myuser 4096 Oct  6 16:34 .git
-rw-rw-r--  1 myuser myuser  653 Oct  4 13:40 .gitignore
drwxrwxr-x  4 myuser myuser 4096 Oct  4 10:16 lib
-rw-rw-r--  1 myuser myuser  744 Oct  4 16:23 notes.txt
drwxrwxr-x 38 myuser myuser 4096 Oct  7 11:13 out
-rw-rw-r--  1 myuser myuser  980 Oct  4 10:16 README.md
-rwxrwxr-x  1 myuser myuser  628 Oct  7 11:04 run.sh
drwxrwxr-x  2 myuser myuser 4096 Oct  6 16:34 script
drwxrwxr-x  2 myuser myuser 4096 Oct  6 15:29 src
drwxrwxr-x  2 myuser myuser 4096 Oct  6 15:29 test
-rwxrwxr-x  1 myuser myuser  182 Oct  4 13:41 wip.sh

Made edits after comments below.

#!/bin/sh
set +x
set +e
. .env
echo "Running flash loan simulation"
echo "PRIVATE_KEY: $PRIVATE_KEY"
echo "SEPOLIA_RPC_URL $SEPOLIA_RPC_URL"
echo "line 9"
cat .env

I have now set the .env permissions to 777 and both files are in the same folder.

$ ls -la
total 68
drwxrwxr-x  9 myuser myuser 4096 Oct  7 12:09 .
drwxrwxr-x  7 myuser myuser 4096 Oct  4 13:39 ..
-rwxrwxrwx  1 myuser myuser  195 Oct  6 16:37 .env
-rwxrwxr-x  1 myuser myuser  163 Oct  7 12:26 run.sh

Running the script gives an error.

$ ./run.sh 
./run.sh: 4: .: .env: not found

Update round 2.

#!/bin/sh
set +x
set +e
#. .env -->.: .env: not found
#..env -->..env: not found
#. ./env -->cannot open ./env: No such file

echo "Running flash loan simulation"
echo "PRIVATE_KEY: $PRIVATE_KEY"
echo "SEPOLIA_RPC_URL $SEPOLIA_RPC_URL"
echo "line 9"
cat .env
7
  • 1
    1) try it without the "clear" to see if you get errors. 2) without an export, don't expect the invoked programs to see it. 3) is that C++ comment actually part of the script? if so, it will mess things up. Commented Oct 7, 2024 at 18:55
  • 1
    ... as well, source is a bashism - if your shebang is #!/bin/sh you should use . .env Commented Oct 7, 2024 at 19:21
  • Please see my updated script and error plus file structure above. Commented Oct 7, 2024 at 19:37
  • Oops my bad - since .env doesn't contain a slash, the shell will search for it on your PATH. Try . ./env instead. See for example bash script error: source: not found (POSIX sh is required to behave the same) Commented Oct 7, 2024 at 19:56
  • 1
    ... that should be . ./.env Commented Oct 7, 2024 at 20:40

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.