This question already has answers here:
Can a shell script set environment variables of the calling shell? [duplicate]
(20 answers)
Closed 3 years ago.
Unable to create environment variables using a tcsh script.
Tried set, but works only inside the script.
setenv doesn't work outside the script.
export says "command not found" in the terminal I'm trying to run.
#!/usr/intel/bin/tcsh
#set WV "/p/hdk/cad/custom_waveview/O-2018.09-SP2/bin/wv"
setenv WV "/p/hdk/cad/custom_waveview/O-2018.09-SP2/bin/wv"
echo $WV
env $WV "/p/hdk/cad/custom_waveview/O-2018.09-SP2/bin/wv"
I expect the output to be /p/hdk/cad/custom_waveview/O-2018.09-SP2/bin/wv, when i echo the environment variable WV on the terminal, but i am getting the error of undefined variable.
Environment variables are set in the current process and inherited by child processes. You can't set environment variables in a parent process.
You have to use the source command to execute the script. That makes the current shell process execute the script itself, rather than running it in a child process.
source env_vars.tcsh
set is for setting shell variables, not environment variables. export is a bash command (and also other shells based on Bourne Shell syntax), not a tcsh command.
env requires the arguments before the program name to be variable settings in the form name=value, e.g.
env VAR1=val1 VAR2=val2 /p/hdk/cad/custom_waveview/O-2018.09-SP2/bin/wv
It runs the program with those variables added to the environment.
Related
I created a shell script which is used to set the environment variables. I am trying to run this script via a python script but every time shows me error as "Command not found". I want to run the shell script as source for setting up the environment variables.
Python : 2.7.5
Script Name : abc.sh
Normal Execution on shell : source abc.sh
Tried using
python : os.system ("source abc.sh")
this shows an error as
"Command not found".
Anybody can help me out how to run this script successfully as source via python ?
The error is because source is a shell "builtin" command -- it is not an external command in your $PATH. But the more fundamental problem is that what you're trying to do won't work due to how environment variables work.
Env vars are private to each process. They are not global and a process cannot modify the env vars of a different process; at least not without the cooperation of the other process. When you start a process it inherits a copy of the env vars of the parent process or the parent provides an explicit set of env vars to the child process it spawns. In either event each process has its own, private, env vars. So even if you did
os.system("sh -c 'source abc.sh'")
it would only modify the env vars of the sh subprocess. It would not modify the environment of the python process.
The simplest solution is to start a shell, do the source abc.sh, then exec your python program. If you absolutely have to set the env vars by running a shell script from within your python program your script will have to write the vars to stdout. Your python program will then have to read that output and parse it to extract the env vars names and value then call os.putenv() to set each var in the python process.
Try your command in command prompt. This because you have not define os in the your command
python : os.system ("source abc.sh"). Your computer is unable access os.system. You first need to install the packages of python which os.system module in it.
I have a shell script setmyenv.sh as below
#!/bin/sh
export PATH=./abc/tools:$PATH
env | grep PATH
When I run it sh setmyenv.sh, I could see that the PATH env is set accordingly.
PATH=./abc/tools:<whatever my existing PATH setting>
However, after my command finish, if I manually type env | grep PATH on the console, I got
PATH=<whatever my existing PATH setting>
I lost the setting that I set using setmyenv.sh
It looks like the environement is only set in the lifetime of my script run.
How could I have the environment set sticky even after the script ended. i.e. the purpose of the script is to set the environment.?
P/S: I don't want to set it in my .bash_profile nor etc\profile, given I only want to set it when needed, by calling setmyenv.sh, but not every time I open my console. i.e. not per the answer of Using .sh script to set an environment variable or How to set global environment variables using shell script .sh
When you run
sh setmyenv.sh
it runs in a separate sh process and the changes to PATH are lost when the process finishes.
You need to source your script:
source setmyenv.sh
or
. setmyenv.sh
so that it runs in your current shell and all variable assignments are preserved. Remember not to have any exit in setmyenv.sh script. If you do, sourcing the script will terminate your shell.
See also:
Difference between sourcing a script vs executing it
What's a subshell
This question already has answers here:
Shell script to set environment variables
(5 answers)
Closed 6 years ago.
We have a shell script named source.sh that exports many ENV variables required for a build.
I am trying to access those ENV variables by running source.sh in my script, but even after running that I am not able to access ENV variables for the succeeding commands.
My Script look alike:
#!/bin/bash
sh source.sh
cd $ROOT_VOS
make
But here cd $ROOT_VOSis not happening
sh source.sh
...runs source.sh in a separate copy of sh (actually, since your parent shell is bash and the child is sh, it's not just a "separate copy", it's an entirely different interpreter). When that separate interpreter exits, local variables and other changes to process-local state are gone. (Environment variables are inherited by child processes; they aren't propagated up the tree to parents).
Instead, use:
# POSIX-compliant: execute every command in source.sh in the current shell
. source.sh
...or...
# bash-specific, perhaps more-readable, synonym for the same
source source.sh
This question already has answers here:
Changing PS1 prompt in a Bash parent shell
(3 answers)
Closed 6 years ago.
I wanted to write a tiny shell script that shortens the commmand prompt when it gets too long. Setting the PS1 variable in bash works fine. When I try the same command directly in a script and run it, nothing happens.
#!/bin/bash
PS1='\u:\W\$ '
I tried eval "PS1='\u:\W\$ '" , export PS1='\u:\W\$ ' and exec PS1='\u:\W\$ ' without any result.
How can I achieve the same result as a direct input in the bash?
Thank you in advance
In general, in UNIX, a process can only change variables for itself and its children -- not its parent ("its parent" being the process that invoked it).
You need to source a script, not execute it, for it to be able to effect your interactive shell's variables. This executes all commands inside the script inside your current shell, not a new shell started as a child process (whose variables' values are thrown away on exit).
# in bash
source yourscript
# or in POSIX sh
. yourscript # mind the space!
In this usage, the shebang does nothing; similarly, the +x permission isn't needed either. It's also typical to name scripts intended to be sourced rather than executed with an extension mapping to the shell they're intended to be used by (yourscript.bash for bash, yourscript.sh for a script which can be sourced by any POSIX shell), whereas scripts intended to be executed rather than sourced should have no extension.
I was trying to set a linux shell environment variable at run time in software using system() command. In the target, couldn't see it set.
for eg.
snprintf(buf, sizeof(buf), "export A=%s", "luck");
system(buf);
Tried /bin/sh -c "export A=B" in the target run time and had no success.
The system call starts a new shell and runs the command in there.
The environment variable will be set in that shell only.
To set an environment variable in your own process, use putenv.