Setting console env using a shell script - linux

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

Related

How to run ssh-agent and ssh-add through an SH script?

I have two following commands which I manually run when I connect on my VPS:
eval ssh-agent $SHELL
ssh-add /home/duvdevan/.ssh/id_rsa
I tried adding them into a ssh.sh and run it within that directory using:
./ssh.sh
But nothing happends.
I'm not that bash-savvy so any help is appreciated.
Instead of running, you need to source the script:
. ./ssh.sh
Otherwise, the environment variables set by the eval command will not be visible in your current shell, and so it cannot know where to find the running ssh agent.
To give a bit more background, here's how this works:
the ssh-agent command starts an ssh agent, and prints to stdout the environment variables you need to set to connect to the agent. The output is formatted as commands to execute. For a test, you can just run this command and see what it prints
the eval command executes the commands printed by ssh-agent. As mentioned earlier, these are commands to set environment variables. After these are executed, the ssh commands you will run in this shell will know where to find the agent
the ssh-add command is able to find the agent, thanks to the environment variables set earlier
these variables are set until the script exits. When you run ./ssh.sh, the variables are set inside the process of that script, and longer available after the script is finished
by sourcing the ssh.sh script using ., the commands inside will be executed in the current shell, and therefore the environment variables are still set, and so your ssh related command can find the agent

Difference with running a script using source and ./ [duplicate]

csh:
set a=0
echo "a is $a"
when i do ./my_script.csh output is:
a is
when i do source my_script.csh output is:
a is 0
Why is it so . As i know that ./ execution uses new shell.
That's right, ./my_script.csh starts a new shell, and uses the #! that you should have at the top of the file to select which shell to run (which should be csh in this case).
source my_script.csh runs the script in the current shell.
If the script is incorrectly run in, for example, the bash shell, set a=0 is not the syntax for setting an environment variable in bash, so the code won't work as you expected, because you're using the wrong shell.
Take a look at the #! at the top of the file. Is it correct?
check if variable "a" is set in your current shell:
set | grep '^a='
Remember that once you source script to your current shell,
all it's global variables are there until unset or you exit the current shell.
You may want to start a new shell, source the script, end exit shell to perform valid tests.
I don't know the context of your problem, but you may want to export some key variables to have their copies in every subprocess.

Shell script running from php code is using /sbin/nologin how to set this to /bin/bash

I am running a shell script by which we are scheduling a task using at command. But it schedules the at task but its not running the same beacuse its using shell /sbin/nologin when we are calling it from php code. It works fine if we run it from terminal.
You should check the "$PATH" env variable. When you are logged in from terminal the shell has initialized it's search path via .bashrc etc. "cron" or "at" jobs don't do that.
So try to log the environment variables to a file in your 'at' jobs and check if it is set up right.

How to set PATH envirnment variable through a shell script though that shell script gets terminated?

I am having a shell script ./my_shellscript.sh, its contents are as follows :
source /path/to/shell_script.sh
Where shell_script.sh contains:
export PATH=/path/to/a/dir:$PATH
which command_name
when I execute the my_shellscript.sh then it shows accurate path to a command, that I executed using "which" command?
When the shell_script.sh terminates and when I again do "which command_name" from command line it doesn't shows any path as it shown when I executed the scripts.
My question is that how to set/persist that path to environment variable [PATH:$PATH] though shell_script.sh terminates?
That's not how environments work, you can't change the parent environment. You can only change your environment, and (optionally) that of child processes to your process.
You could run your ./my_shellscript.sh with source (or .) to export it's variables to your current environment.
source my_shellscript.sh
or
. my_shellscript.sh
Other option to put the PATH variable extension into your .profile file in your home directory. (/home/your_username/.profile) That will be permanent.

How to make declare in a Linux shell script?

I want to put below declare in a shell script: proxy_set
declare -x https_proxy="https://192.168.220.4:8080/"
And then I execute it like below.
$ ./proxy_set
But "export" shows nothing happened.
And in another way if I execute it like this:
$ source proxy_set
Then "export" shows it works!
My question is how can I make it work without additional "source" cmd?
Thanks!
You can't. Setting variables in the environment only affects the environment of that shell and any future children it spawns; there's no way to affect the parent shell. When you run it without the source (or .), a brand new shell is started up, then the variable is set in that shell's environment, and then that shell exits, taking its environment with it.
The source reads the commands and executes them within the current shell as if you had typed them.
So if you want to set environment variables in a script, you have to source it. Alternatively, you can have a command generate shell commands as output instead of running them, and then the parent can evaluate the output of the command. Things like ssh-agent use this approach.
Try just adding:
export https_proxy="https://192.168.220.4:8080/"
Then execute your script normally.

Resources