Linux modify ~/.bashrc or /etc/profile to set environment variables

Introduction

       Installing some software on the virtual machine Linux system often encounters situations where you need to set environment variables yourself. Anyone who has installed Anaconda on Windows knows that environment variables are a very important and magical thing (it feels like Anaconda is a special python virtual machine). But compared to Linux, setting environment variables on Windows is much more convenient.
       This blog mainly combines the blogger's installation of VCS on the CentOS7 Linux system (the subsequent installation will take a screenshot and a blog post when I have time) and the experience of VARON to summarize the setting of environment variables on Linux. This article will teach you to understand environment variables.

What are environment variables

       During the installation of VCS, the original or reprinted blog on CSDN will say to modify the .bashrc setting environment variables without moving. Some lazy bloggers even directly said that we need to set the environment variable balabala here. I didn’t know it at the time, just install the package. The .bashrc file modified in the directory where it is located is successfully installed. After the terminal source in that directory, you can also open the VCS suite such as verdi, but the external software will never succeed in calling VCS. In the end it comes down to the issue of environmental variables.

       So here we need to explain the environment variable first. In other words, you want to call a file under a certain path under any path, but you can't always enter a lot of path names every time. At this time, you need to use environment variables. , After setting the PATH, which is the environment variable, in the relevant file (detailed below), you can call it directly by name no matter where or in which external application (generally, there is no conflict between compliant names, of course, there is also the same There will be conflicts when different versions of the software are installed, but the default one is usually opened).

How to set environment variables

       Here we mainly talk about the setting of PATH, the most common environment variable, as well as LD_LIBERARY_PATH, but these will be the same after clearing his meaning. This PATH is the same thing as the PATH on Windows. If you want to open verdi directly without entering the path, add the verdi path to the PATH.

export SynopsysList=/home/lmh/Synopsys
export VERDI_HOME=$SynopsysList/verdi
export PATH=$PATH:$VERDI_HOME/bin

$ Is a bit like a pointer. In addition to some proprietary names, you can define some common path names such as VCS_HOME, VERDI_HOME, which are mainly convenient for direct calling in other paths. Adding $ means that the path is called. Direct=Yes The path is redefined, so the original path $PATH must be added when PATH is defined, and the paths are connected by colons.

So look at this again to understand a lot, alias is also similar to define things, I added or unquoted single quotes and double quotes on CentOS, it doesn't matter, finally lmg_vcs can activate the license.

export SynopsysList=/home/lmh/Synopsys
export DVE_HOME=$SynopsysList/vcs/gui/dve
export VCS_HOME=$SynopsysList/vcs
export VERDI_HOME=$SynopsysList/verdi
#dve
export PATH=$PATH:$VCS_HOME/gui/dve/bin
alias dve="dve -full64"
#VCS
export PATH=$PATH:$VCS_HOME/bin
alias vcs="vcs -full64"
#VERDI
export PATH=$PATH:$VERDI_HOME/bin
alias verdi="verdi -full64"
#scl
export PATH=$PATH:$SynopsysList/scl/amd64/bin
export VCS_ARCH_OVERRIDE=linux
#LICENCE
export [email protected]
alias lmg_vcs="lmgrd -c $SynopsysList/vcs/license/Synopsys.dat"

By the way, in general, the license that needs to be manually activated by yourself needs to be hung up. Of course, there are also lazy ways to directly modify the /etc/rc.d/rc.local file and add LD_LIBERARY_PATH to it. I don’t understand. , Honestly, manual activation is good.

.bashrc

Bash is the bash of the shell, there are many on CSDN, just learn it, rc is the run command, literally.

There is also a .brashrc file. I don’t know the difference with this .bashrc. I tried to change the PATH in .brashrc and the PATH did not change after the source. The Internet did not say the difference between the two. If you know it, you can Leave a reply.

Here is a direct picture to show you the .bashrc file in the subdirectory:

As shown in the figure above, after opening the shell under this path, there is no path such as vcs, but the source.bashrc will show the path of vcs when you view the $PATH later. At this time, you can directly enter the VCS command that needs to be input, but only on the left This terminal window does not work on the right.

The .bashrc file in the subdirectory will not be preloaded and will not be public. It is only for the current terminal window of the current user. Each call is equivalent to source the .bashrc file in this path. Somewhat similar to temporary environment variables.

Therefore, it is generally required to change ~/.bashrc or /etc/profile to set environment variables. The difference between the two is explained below:

~/ is to enter the home directory of the current user. I.e. /home/<user_name> path
 

.bashrc is to enter the .bashrc folder, which is the directory named .bashrc under the user directory.

The difference between bashrc and profile:

To understand the difference between bashrc and profile, we must first understand what is an interactive shell and a non-interactive shell, and what is a login shell and a non-login shell.

Interactive mode is that the shell waits for your input and executes the commands you submit. This mode is called interactive because the shell interacts with the user. This mode is also very familiar to most users: login, execute some commands, sign out. When you sign out, the shell is also terminated. The shell can also run in another mode: non-interactive mode. In this mode, the shell does not interact with you, but reads the commands stored in the file and executes them. When it reaches the end of the file, the shell terminates.

Both bashrc and profile are used to save user environment information, bashrc is used for interactive non-login shell, and profile is used for interactive login shell. There are many bashrc and profile files in the system. There are two main types (the settings of general environment variables only use these two):

  • /etc/profile This file sets up environmental information for each user of the system. When the first user logs in, this file is executed. And collect shell settings from the configuration file /etc/profile.d.
  • ~/.bashrc: This file contains bash information dedicated to a user's bash shell. This file is read when the user logs in and every time a new shell is opened.

In addition, the variables (global) set in /etc/profile can act on any user, while the variables (local) set in ~/.bashrc etc. can only inherit the variables in /etc/profile, they are "father and child" "relationship.

to sum up

At present, bloggers mainly only use these two ways of setting environment variables /etc/profile and ~/.bashrc, and they can solve most of the software environment variable problems. /etc/profile is only executed once when the user logs in. You need to re-source after this modification. It is best to see if the path is added to open a new window after modification; ~/.bashrc will be executed every time a user logs in and opens a terminal window.

Guess you like

Origin blog.csdn.net/weixin_49617016/article/details/108749834