Issue with setting environment variables permanently in linux bash [closed] - linux

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I want to set up a global environment variable permanently into the shell, so i do not set it every time i open another shell or another log-in session.
I have set the variable using export as following:
$ export pass='my_pass'
However when i use another active shell to restore this variable using echo as following:
$ echo $pass
The variable does not exist, so it only exist in the local shell of setup.
I have tried putting it into the .bash_profile but this also did not work.

~/.bash_profile is only sourced on login (i.e. after you've typed your username & password) - ~/.bashrc is sourced for interactive non-login shells.
So I'd add the variables into ~/.bashrc (don't forget to source it first if you're running the python script afterwards from the same shell). This way, when you open a new shell, bashrc will be sourced and your environment variables will be available.
Edit:
As others have said in comments .. running an export command in one shell, won't make that variable availble in another shell - you need to add it to your ~/.bashrc to make it avaiable in other shells

Related

How can I run a command in a shell script function that requires access to the current shell instance? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
I am using a custom package manager called spack, which allows me to load installed modules using the spack load command. It is similar to the familiar module load command in many ways. I am using zsh.
I have set up a shell script with a function that I would later like to insert into my .zshrc file. It is currently located in a standalone file for testing purposes, which looks as following:
#!/bin/bash
load-standard () {
echo "loading $1"
spack load $1
}
load-standard $1
When I run this script with source ./script_name package_name, I get an error message that says
`spack load` requires Spack's shell support.
To enable Spack's shell support, a file called setup-env.sh must be run which enables the user to make use of the spack command.
However, directly typing in the commands spack load package_name works with no problem.
I always assumed that running a command from a shell script is the same as typing it into the current shell. How can I make my shell interpret the spack load commands exactly as if I had directly typed them in?
EDIT: Placing the function in my .zshrc file solved this problem.
I'm not familiar with spack, but likely spack is a shell function which modifies the current shell environment. That is how module works. type spack to check.
You can't modify the shell environment from a script, you can from a shell function.
Copy and paste the function load-standard to "$ZDOTDIR/.zshrc" (for current user, /etc/zshrc for all users), source .zshrc (. "$ZDOTDIR/.zshrc") and you should be fine (no need to restart).
You can also create a list of functions in a file, and add . /path/to/functions to zshrc, to source it.

Execute the shell script that is in the current directory, not the one in $PATH [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have defined the path in .bash_profile file for /usr/share/totalscripts as:
PATH="${PATH}:/usr/share/totalscripts"
export PATH
Where all my shell scripts programs are present - /usr/share/totalscripts
Specifically, I need to execute the scripts in the current directory, not the one available in my $PATH environment variable. That is, executing a specific script rather than any script that matches that is found in the PATH.
For example, it might make sense to not call script1.sh (which will execute against any script1.sh found in the PATH), but instead call ./script1.sh (which will only execute the script that is found in the local directory).
This because I have the same script under /usr/share/totalscripts/script1.sh and /home/script1.sh (same names)
If you are working in a script, then you can define your environment variables at the start of it, like:
MY_VAR="/my/path/to/scripts"
And then call your script from within that script using:
${MY_VAR}/my_script.sh
Kind of the same as running the script using its full path.
I hope this helps!
Edit 1: - If you want to run a specific script as you mentioned in the comments, under a specific directory, you can try changing directories to the one that contains the file, and then running the script like ./script1.sh.
cd /path/containing/your/scripts/
./script1.sh
Edit 2: - Maybe you can create a wrapper for your script1.sh, placing the following if-block in there to check if the script is under the current directory or not.
if [ -e script1.sh ]; then
./script1.sh # runs the script under your current directory
else
script1.sh # runs the script searching for it in $PATH first
fi

Store frequently used commands in Linux [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I use some Linux commands more frequently like going to a specific directory, grep for some text etc. I assigned a variable for each command and kept them in a script file and I run the script file every time when I login to my Linux box so that I can use the variables instead of typing lengthy commands.
Is it possible that I can make sure that my script file runs everytime when I login to my Linux box so that I need not run it everytime?
Is there an alternate way of storing my frequently used commands so that they will be available when I open my Linux box?
If you are using bash (probably are), add it to your .bashrc. You will find it in your home directory.
Other shells have corresponding startup scripts.
Adding commands to .bashrc for a non-login shell, or to .bash_profile for login shells (assuming, of course, that you're using bash).
From the bash manual entry:
When bash is invoked as an interactive login shell, or as a
non-interactive shell with the --login option, it first reads and
executes commands from the file /etc/profile, if that file exists.
After reading that file, it looks for ~/.bash_profile, ~/.bash_login,
and ~/.profile, in that order, and reads and executes commands from
the first one that exists and is readable. The --noprofile option may
be used when the shell is started to inhibit this behavior.
When a login shell exits, bash reads and executes commands from the
file ~/.bash_logout, if it exists.
When an interactive shell that is not a login shell is started, bash
reads and executes commands from ~/.bashrc, if that file exists. This
may be inhibited by using the --norc option. The --rcfile file option
will force bash to read and execute commands from file instead of
~/.bashrc.
You have to put your script into your .bashrc file, it is in your home directory
nano ~/.bashrc
It only works when you are using bash.
What is about alias?
You can store them in the ~/.bashrc, when I am right.
You can use a .bashrc file but this script is executed when you open an interactive Bash shell. That is every time you connect to a server with a terminal (if Bash is your default shell) or open another shell that opens an interactive shell (like su - $USER).
If you work locally with X-Window GUI on Linux (Unix) the script will be executed every time you open a terminal program (like Konsole in KDE or gnome-terminal). It maybe not what you expected. In this case you can hack a .xinit script or use your display manager or desktop environment way to execute a script upon start. It is hard to tell how because it is specific to your environment (Linux/ Unix distribution or desktop environment (KDE, GNOME, ...) ) .

How to permanently set $PATH on Linux/Unix [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
Improve this question
On Linux, how can I add a directory to the $PATH so it remains persistent across different sessions?
Background
I'm trying to add a directory to my path so it will always be in my Linux path. I've tried:
export PATH=$PATH:/path/to/dir
This works, however each time I exit the terminal and start a new terminal instance, this path is lost, and I need to run the export command again.
How can I do it so this will be set permanently?
You need to add it to your ~/.profile or ~/.bashrc file.
export PATH="$PATH:/path/to/dir"
Depending on what you're doing, you also may want to symlink to binaries:
cd /usr/bin
sudo ln -s /path/to/binary binary-name
Note that this will not automatically update your path for the remainder of the session. To do this, you should run:
source ~/.profile
or
source ~/.bashrc
There are multiple ways to do it. The actual solution depends on the purpose.
The variable values are usually stored in either a list of assignments or a shell script that is run at the start of the system or user session. In case of the shell script you must use a specific shell syntax and export or set commands.
System wide
/etc/environment List of unique assignments. Allows references. Perfect for adding system-wide directories like /usr/local/something/bin to PATH variable or defining JAVA_HOME. Used by PAM and systemd.
/etc/environment.d/*.conf List of unique assignments. Allows references. Perfect for adding system-wide directories like /usr/local/something/bin to PATH variable or defining JAVA_HOME. The configuration can be split into multiple files, usually one per each tool (Java, Go, and Node.js). Used by systemd that by design do not pass those values to user login shells.
/etc/xprofile Shell script executed while starting X Window System session. This is run for every user that logs into X Window System. It is a good choice for PATH entries that are valid for every user like /usr/local/something/bin. The file is included by other script so use POSIX shell syntax not the syntax of your user shell.
/etc/profile and /etc/profile.d/* Shell script. This is a good choice for shell-only systems. Those files are read only by shells in login mode.
/etc/<shell>.<shell>rc. Shell script. This is a poor choice because it is single shell specific. Used in non-login mode.
User session
~/.pam_environment. List of unique assignments, no references allowed. Loaded by PAM at the start of every user session irrelevant if it is an X Window System session or shell. You cannot reference other variables including HOME or PATH so it has limited use. Used by PAM.
~/.xprofile Shell script. This is executed when the user logs into X Window System system. The variables defined here are visible to every X application. Perfect choice for extending PATH with values such as ~/bin or ~/go/bin or defining user specific GOPATH or NPM_HOME. The file is included by other script so use POSIX shell syntax not the syntax of your user shell. Your graphical text editor or IDE started by shortcut will see those values.
~/.profile, ~/.<shell>_profile, ~/.<shell>_login Shell script. It will be visible only for programs started from terminal or terminal emulator. It is a good choice for shell-only systems. Used by shells in login mode.
~/.<shell>rc. Shell script. This is a poor choice because it is single shell specific. Used by shells in non-login mode.
Notes
GNOME on Wayland starts a user login shell to get the environment. It effectively uses the login shell configurations ~/.profile, ~/.<shell>_profile, ~/.<shell>_login files.
Man pages
environment
environment.d https://linux.die.net/man/1/environment.d
bash
dash
Distribution-specific documentation
Ubuntu
Arch Linux
Related
Difference between Login Shell and Non-Login Shell?
In Ubuntu, edit /etc/environment. Its sole purpose is to store environment variables. Originally the $PATH variable is defined here.
This is a paste from my /etc/environment file:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
So you can just open up this file as root and add whatever you want.
For immediate results,
Run (try as normal user and root):
source /etc/environment && export PATH
If you use Z shell (zsh), add this line right after the comments in /etc/zsh/zshenv file:
source /etc/environment
I encountered this little quirk on Ubuntu 15.10 (Wily Werewolf), but if your zsh is not getting the correct PATH, this could be why.
For Bash, you can put the export declaration in ~/.bashrc. For example, my .bashrc contains this line:
export PATH=/var/lib/gems/1.8/bin:/home/ash/.bin:$PATH
You may set $PATH permanently in two ways.
To set the path for a particular user:
You may need to make the entry in file .bash_profile in the home directory for the user.
E.g, in my case I will set the java path in the Tomcat user profile*
echo "export PATH=$PATH:/path/to/dir" >> /home/tomcat/.bash_profile
To set a common path for all system users, you may need to set the path like this:
echo "export PATH=$PATH:/path/to/dir" >> /etc/profile
You can use on CentOS or Red Hat Linux (RHEL) for the local user:
echo $"export PATH=\$PATH:$(pwd)" >> ~/.bash_profile
This adds the current directory (or you can use another directory) to the PATH. This makes it permanent, but it takes effect at the next user logon.
If you don't want do a re-logon, then you can use:
source ~/.bash_profile
That reloads the # User specific environment and startup programs. This comment is present in file .bash_profile.
You can also set it permanently, editing one of these files:
/etc/profile (for all users)
~/.bash_profile (for current user)
~/.bash_login (for current user)
~/.profile (for current user)
You can also use /etc/environment to set a permanent PATH environment variable, but it does not support variable expansion.
Extracted from: Linux: Añadir ruta al PATH
I think the most elegant way is:
Add this in the ~/.bashrc file.
Run this command:
gedit ~/.bashrc
Add your path inside it:
export PATH=$PATH:/opt/node/bin
source ~/.bashrc
(Ubuntu)
Modify the "/etc/profile" file:
vi /etc/profile
Press the I key to enter editing mode and move the cursor to the end of the file. Additional entries:
export PATH=$PATH:/path/to/dir;
Press the Esc key to exit edit mode, and :wq to save the file.
Make the configuration effective
source /etc/profile
Explanation:
The profile file works for all users. If you want it to be valid only for the active user, change the ".bashrc" file.
I stumbled across this question yesterday when searching for a way to add a folder containing my own scripts to the PATH - and was surprised to find out that my own ~/.profile file (on Linux Mint 18.1) already contained this:
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
Thus, all I had to do was create the folder ~/bin and put my scripts there.
You can add that line to your console configuration files (e.g., .bashrc, or to .profile).
After so much research, I found a simple solution for this (I am using Elementary OS), inspired by Flutter – Step by Step Installation on Linux – Ubuntu.
Run the following command to open the .bashrc file in edit mode. (You
may also use vi or any other editor).
~$ sudo nano ~/.bashrc
Add the following line at the end of the file and save.
export PATH="[FLUTTER_SDK_PATH]/flutter/bin:$PATH"
For example:
export PATH="/home/rageshl/dev/flutter/bin:$PATH"
I believe this is the permanent solution for setting the path in Flutter in a Ubuntu distribution.
It can be directly added by using the following command:
echo 'export PATH=$PATH:/new/directory' >> ~/.zshrc
source ~/.zshrc
One way to add a permanent path, which worked for me, is:
cd /etc/profile.d
touch custom.sh
vi custom.sh
export PATH=$PATH:/path according to your setting/
Restart your computer and here we go; the path will be there permanently.
Add script file [name_of_script].sh to the /etc/profile.d folder with the line:
export PATH=$PATH:/dir
Every script within the /etc/profile.d folder is automatically executed by /etc/profile on login.
My answer is in reference to the setting up of a Go environment on Ubuntu Linux (amd64). I have faced the same trouble of setting the path of environment variables (GOPATH and GOBIN), losing it on terminal exit and rebuilding it using the source <file_name> every time.
The mistake was to put the path (GOPATH and GOBIN) in ~/.bash_profile file. After wasting a few good hours, I found that the solution was to put GOPATH and GOBIN in the ~/.bash_rc file in the manner:
export GOPATH=$HOME/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOPATH:$GOBIN
And in doing so, the Go installation worked fine and there were no path losses.
The reason with which this issue can be related is that settings for non-login shells, like your Ubuntu terminal or GNOME terminal where we run the Go code, are taken from the ~./bash_rc file and the settings for login shells are taken from ~/.bash_profile file. And from the ~/.profile file if the ~/.bash_profile file is unreachable.
The files where you add the export command depends on if you are in login-mode or non-login-mode.
If you are in login-mode, the files you are looking for are either /etc/bash or /etc/bash.bashrc.
If you are in non-login-mode, you are looking for the file /.profile or for the files within the directory /.profiles.d
The files mentioned above is where the system variables are.
Permanently add to the PATH variable
Global:
echo "export PATH=$PATH:/new/path/variable" >> /etc/profile
Local (for the current user only):
echo "export PATH=$PATH:/new/path/variable" >> ~/.profile
For global, restart. For local, relogin.
Example
Before:
$ cat /etc/profile
#!/bin/sh
export PATH=/usr/bin:/usr/sbin:/bin:/sbin
After:
$ cat /etc/profile
#!/bin/sh
export PATH=/usr/bin:/usr/sbin:/bin:/sbin
export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/new/path/variable
Alternatively you can just edit file "profile":
$ cat /etc/profile
#!/bin/sh
export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/new/path/variable
Another way (thanks gniourf_gniourf):
echo 'PATH=$PATH:/new/path/variable' >> /etc/profile
You shouldn't use double quotes here! echo 'export
PATH=$PATH:/new/path/variable'... And by the way, the export keyword
is very likely useless as the PATH variable is very likely already
marked as exported. – gniourf_gniourf
Zues77 has the right idea. The OP didn't say "How can I hack my way through this?". The OP wanted to know how to permanently append to $PATH:
sudo nano /etc/profile
This is where it is set for everything and is the best place to change it for all things needing $PATH.
Let's say you're running macOS. You have a binary you trust and would like to make available across your system, but don't necessarily want the directory in which the binary is to be added to your PATH.
You can opt to copy/move the binary to /usr/local/bin, which should already be in your PATH. This will make the binary executable like any other binary you may already have access to in your terminal.
The simplest way is the following line,
PATH="<directory you want to include>:$PATH"
in your .bashrc file in the home directory.
It will not get reset even if you close the terminal or reboot your PC. It's permanent.
This is a one-liner. It adds a line to the .bashrc. That line is going to check if the directory has already been added to the path and append if not. This will prevent duplicating your directory in the path every time you source .bashrc.
echo "[[ \":\$PATH:\" != *\":$(pwd)/path/to/add:\"* ]] && export PATH=\"\${PATH:+\${PATH}}:$(pwd)/path/to/add\"" >> ~/.bashrc
source ~/.bashrc
I think the most elegant way is:
Add this in the ~./bashrc file:
if [ -d "new-path" ]; then
PATH=$PATH:new-path
fi
source *~/.bashrc*
(Ubuntu)
For a Debian distribution, you have to:
edit file ~/.bashrc. E.g: vim ~/.bashrc
add export PATH=$PATH:/path/to/dir
then restart your computer. Be aware that if you edit file ~/.bashrc as root, your environment variable you added will work only for root

Make $JAVA_HOME easily changable in Ubuntu [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
In Ubuntu, I'd like to switch my JAVA_HOME environment variable back and forth between Java 5 and 6.
I open a terminal and type in the following to set the JAVA_HOME environment variable:
export JAVA_HOME=/usr/lib/jvm/java-1.5.0-sun
And in that same terminal window, I type the following to check that the environment variable has been updated:
echo $JAVA_HOME
And I see /usr/lib/jvm/java-1.5.0-sun which is what I'm expecting to see. In addition, I modify ~/.profile and set the JAVA_HOME environment variable to /usr/lib/jvm/java-1.5.0-sun.
And now for the problem--when I open a new terminal window and I check my JAVA_HOME environment variable by typing in echo $JAVA_HOME I see that my JAVA_HOME environment variable has been reverted back to Java 6. When I reboot my machine (or log out and back in, I suppose) the JAVA_HOME environment variable is set to Java 5 (presumably because of the modification I made in my ~/.profile).
Is there a way around this so that I can change my JAVA_HOME environment without having to log out and back in (AND make that environment variable change stick in all new terminal windows)?
Put the environment variables into the global /etc/environment file:
...
export JAVA_HOME=/usr/lib/jvm/java-1.5.0-sun
...
Execute "source /etc/environment" in every shell where you want the variables to be updated:
$ source /etc/environment
Check that it works:
$ echo $JAVA_HOME
$ /usr/lib/jvm/java-1.5.0-sun
Great, no logout needed.
If you want to set JAVA_HOME environment variable in only the terminal, set it in ~/.bashrc file.
This will probably solve your problem:
https://help.ubuntu.com/community/EnvironmentVariables
Session-wide environment variables
In order to set environment variables in a way that affects a particular user's environment, one should not place commands to set their values in particular shell script files in the user's home directory, but use:
~/.pam_environment - This file is specifically meant for setting a user's environment. It is not a script file, but rather consists of assignment expressions, one per line.
Not recommended:
~/.profile - This is probably the best file for placing environment variable assignments in, since it gets executed automatically by the DisplayManager during the startup process desktop session as well as by the login shell when one logs-in from the textual console.
Try these steps.
--We are going to edit "etc\profile".
The environment variables are to be input at the bottom of the file. Since Ubuntu does not
give access to root folder, we will have to use a few commands in the terminal
Step1: Start Terminal. Type in command: gksudo gedit /etc/profile
Step2: The profile text file will open. Enter the environment variables at the bottom of the page........... Eg: export JAVA_HOME=/home/alex/jdk1.6.0_22/bin/java
export PATH=/home/alex/jdk1.6.0_22/bin:$PATH
step3: save and close the file. Check if the environment variables are set by using echo command........ Eg echo $PATH
You need to put variable definition in the ~/.bashrc file.
From bash man page:
When an interactive shell that is
not a login shell is started, bash
reads and executes commands from
/etc/bash.bashrc and ~/.bashrc, if
these files exist.
Traditionally, if you only want to change the variable in your terminal windows, set it in .bashrc file, which is sourced each time a new terminal is opened. .profile file is not sourced each time you open a new terminal.
See the difference between .profile and .bashrc in question:
What's the difference between .bashrc, .bash_profile, and .environment?
.bashrc should solve your problem. However, it is not the proper solution since you are using Ubuntu. See the relevant Ubuntu help page "Session-wide environment variables". Thus, no wonder that .profile does not work for you. I use Ubuntu 12.04 and xfce. I set up my .profile and it is simply not taking effect even if I log out and in. Similar experience here. So you may have to use .pam_environment file and totally forget about .profile, and .bashrc. And NOTE that .pam_environment is not a script file.
Take a look at bash(1), you need a login shell to pickup the ~/.profile, i.e. the -l option.
I know this is a long cold question, but it comes up every time there is a new or recent major Java release. Now this would easily apply to 6 and 7 swapping.
I have done this in the past with update-java-alternatives:
http://manpages.ubuntu.com/manpages/hardy/man8/update-java-alternatives.8.html
After making changes to .profile, you need to execute the file, in order for the changes to take effect.
root#masternode# . ~/.profile
Once this is done, the echo command will work.

Resources