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
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.sourceis a bashism - if your shebang is#!/bin/shyou should use. .env.envdoesn't contain a slash, the shell will search for it on yourPATH. Try. ./envinstead. See for example bash script error: source: not found (POSIX sh is required to behave the same). ./.env