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!
Related
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.
I've searched for an answer and read about different ways to do that in Mac but some of them are not relevant for Mojave or just didn't work for me.
I need to set Environment variable in terminal (bash), run script that creates processes, and I would like those processes to know the value of those environment variables.
How can I do that?
btw - writing export ENV_NAME=ENV_VAL in .bashrc or in .bash_profile didn't work.
Works for me. Have you RTFM? For example, ~/.bashrc is only read by interactive shells, not shell scripts. And ~/.bash_profile is only read by login shells. Again, shell scripts don't usually use the -l flag that would make them login shells. Also, if you put an export VAR=value statement in your ~/.bashrc it won't affect your current interactive shell. You need to start a new shell; e.g., by typing exec bash. Once you do that you should find the env var is defined. And it will be inherited by any process, including a shell script, you launch from that interactive session.
Note that if you run your script via crontab, for example, then you'll need a different means of setting the env var. For example, by using the --init-file flag or the BASH_ENV env var.
OS: Ubuntu 18.04
I am trying to use my /etc/environment file, to export some variables, to be used by Rails.
cat /etc/environment
....
RAILS_ENV='test'
RAILS_DB_PWD='X35i#98n'
However, when I try:
echo $RAILS_DB_PWD
I get:
X35i
It looks like it's cut off at the #. I would like to include # in the password, and make it available system wide, not just from my local bash shell.
However, if I add this to my .bashrc file, it works fine
Any ideas?
What does shopt show?
echo "${RAILS_DB_PWD}"
Should prevent the shell from interpreting the # that way.
As Elias Soares said in his comment, /etc/environment is not a good place to set a variable with embedded #. Set it in your bash initialization file (for instance /etc/profile) as
RAILS_DB_PWD='X35i#98n'
and it should work. If you intend to span (non-bash) subshells or run programs you are supposed to use this environment variable, don't forget to export it.
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
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