Temporarily setting an environment variable in tcsh - tcsh

I do something like this in bash to set LD_LIBRARY_PATH for only one command, without leaving it in the environment afterwards:
LD_LIBRARY_PATH=/opt/geometry ./ex3_3.out
What's the equivalent in tcsh? If I use setenv LD_LIBRARY_PATH /opt/geometry; ./ex3_3.out it leaves LD_LIBRARY_PATH set, which is not what I want.

/usr/bin/env LD_LIBRARY_PATH=/opt/geometry ./ex3_3.out
...should work everywhere, since it's relying on /usr/bin/env rather than any shell-specific syntax or builtin.

Related

Accessing environment variables in bash script

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.

Set global Environment variable on Mac OS Mojave

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.

What effect does /usr/bin/env have?

What's the difference between #! /usr/bin/env ruby and #! ruby?
(I found many other questions discussing the difference between #! /usr/bin/env ruby and #! /usr/bin/ruby, but that's not my question.)
#! ruby
...is not guaranteed to work on UNIXlike systems (and does not work on any I personally know of) at all; a valid shebang must have a fully qualified path. It may suffice to tell your editor which programming language you're using, but that doesn't mean that the kernel will successfully use it to select an interpreter with which to run a program.
The kernel's execve syscall doesn't do PATH lookups -- that's added by C-standard-library wrappers such as execlp and execvp, but parsing shebangs is done directly by the kernel, so your C-library nicities don't happen there.
#!/usr/bin/env ruby
...uses the PATH to look up the location of the ruby executable. Because the path to the env executable is fully specified, this is a valid shebang line (which #! ruby is not).
env has other purposes as well -- you can run, for instance, env -i someprog to run someprog with a completely empty environment, or env FOO=bar someprog to run someprog with the environment variable FOO set to the value bar (which FOO=bar someprog would also do if running through a shell, but the env approach also works with no shell involved).
However, the relevant use case in this context is forcing a PATH lookup.

bash script not picking up environment variables

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!

Shell script problem to set my env

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

Resources