Before I run my Flask application using
$ flask run
I need to set few environment variables (e.g. FLASK_APP pointing to the application file). I've been trying to automate the process with a .sh script:
export FLASK_APP=application.py
export FLASK_DEBUG=1
echo $FLASK_APP
echo $FLASK_DEBUG
It turns out that even if the last two lines print out the input from the first two lines, Flask doesn't seem to care. If I want to set the variables so that Flask changes its behaviour, I must do it manually by typing the first two lines from the script in the terminal:
$ export FLASK_APP=application.py
$ export FLASK_DEBUG=1
What am I doing wrong when it comes to the script?
You are probably running your shell script as:
$ path_to_your_script.sh
This loads another shell and executes a script.
If you want to set environment variables for you current shell, use:
$ source path_to_your_script.sh
or (dot at the beginning)
$ . path_to_your_script.sh
Related
I am working on a chalice project. I need to set environment variables locally in order to connect to the DB using peewee.
I wrote a simple shell script called env.sh:
#!/bin/sh
export APP_NAME=example
export DB_HOST=example
export DB_PORT=example
export DB_NAME=example
export DB_USERNAME=example
export DB_PASSWORD=example
I expected that running ./env.sh will set all the env variables. But when I execute the chalice application, I get errors indicating that these variables are not defined.
If I go into the terminal and run these commands individually like export APP_NAME=example, the variables are set and the application successfully reads them.
I am using a virtual env with poetry (poetry shell) with python3.9, so it might have to do with that, but I am not sure how to wrap my head around this. I also tried poetry run env.sh and removed the shebang from the top of the script, but none of these worked.
Can you help me figure out why this is happening and how to make that script work?
I have a bash script where I am trying to use values of some environment variables. Those variables are defined - I see the keys and values if I run printenv.
Also, these variables are defined and exported like
export FOO="bar"
in both ~/.bash_profile and ~/.bashrc.
I am trying to execute the script via ./script-name which fails to get the environment variables. If I run sudo -E ./script-name, that somehow gets the script the variables it needs.
Confused as to why these variables aren't available to the script even when they are exported in above files.
The only thing I can think of, is that for some reason, the shell process which you are calling to run the script, does not have full read access to your current environment.
ls -al /usr/bin/bash
ls -al /bin/sh
Assuming neither of them are symlinks, make sure that your current user has read and execute priveleges. A safer (in security terms) option, would be for you to install bash in ~/opt, and use #!~/opt/bin/bash as your shebang line.
When zsh was exporting a PATH from a script, it didn't read the path correctly.
My PATH was export PATH="~/path/to/stuff/", but when I tried to run a command located at that path, zsh could not find it.
When I changed the PATH to export PATH="$HOME/path/to/stuff/", then the zsh was able to run the command.
EDIT: The strange thing is that I just checked this and it's working again with export PATH="~/path/to/stuff/". There must be something weird going on with my dev environment.
EDIT 2: I failed to mention earlier that the script I am reading export PATH="~/path/to/stuff/" from is building a local dev environment for a team of developers who mainly use bash as their shell. I prefer to use zsh so I have to get my shell to play nice with all of the configs for the dominant bash setup across the team.
Use the following code to get what you want:
export PATH=~/Desktop/Capture/
echo $PATH
# Result: /Users/swift/Desktop/Capture/
Although, when you're using a string, you'll get this:
export PATH="~/Desktop/Capture/"
echo $PATH
# Result: ~/Desktop/Capture/
So to get it right, you'll have to try this approach:
tilde=~
export PATH="${tilde}/Desktop/Capture/"
echo $PATH
# Result: /Users/swift/Desktop/Capture/
P.S. Also, there's one useful command for tilde to be expanded.
Here's an example:
echo tilda=~
# Result: tilda=~
Use magicequalsubst command in zsh:
set -o magicequalsubst
echo tilda=~
# Result: tilda=/Users/swift
I have a strange situation where I'm using zsh full-time, and any bash scripts I run are not picking environment variables properly. Obviously I don't expect bash to pick up env vars that are defined in zsh's environment, so I am using ~/.bashrc and ~/.bash_profile, but that doesn't work either.
For example, here's a test script:
#!/bin/bash
echo $MYTEST
I've added this line to both ~/.bashrc and ~/.bash_profile to cover my bases:
export MYTEST="hello"
I just get a blank line when running the script.
PS: I know running . ./testscript will work, but that's not an option since it's a system-wide script that's failing to pull env vars.
Oops. Maybe I should try having export VAR=val in my ~/.oh-my-zsh/custom/vars.zsh instead of just VAR=val!
We have few executable which need some environment setting.
We manually running those scripts before running the executable
Like
$ . setenv.ksh
We have to encompass call these in one script to avoid the manual work.
We written a sh script like
#!/bin/sh
. setenv.ksh
./abc &
Still the environments are not setting in that session. I think the “. setenv.ksh” runs with fork and it’s not setting the environment.
Please me to solve this problem. Which command we use to run the setenv.ksh so, this will work fine.
Thanks
I notice the environment script is called setenv.ksh but you try to run it from /bin/sh. Maybe your system has a shell other than ksh as /bin/sh and it misparses something it setenv.ksh. Try changing the shebang line to #!/bin/ksh (or whatever the path to ksh is on your system).
In setenv.ksh, you need to export all environment variables you set so that any sub-shell will inherit the values:
export MYENV=myValue