background
Many students may have a headache when they see the environment variables. They do not know how to match and do not know what to change
Once you want to configure environment variables, you can browse the Internet and copy them randomly
So I’m going to summarize the environment configurations and commands I use in my work, based on my knowledge
If there’s anything wrong, the big guys are spitting in the comments section
I use a Mac, most of the following introduction is based on Mac and Linux, Windows brother for reference only
SHELL Environment introduction
First we need to introduce the SHELL environment
We open the environment inside the black box that appears in “terminal” (some of you use iterm2 or other terminal tools) and call it the SHELL environment
There are many different SHELL environments, the most common being Bash and ZSH
Bash and ZSH
In early Macs, the default shell environment was bash
On today’s new Macs, the default shell environment is ZSH
Of course, if you don’t know what SHELL environment you are in, you can use the following command to check
echo $SHELL
Copy the code
Older Macs that install the new OS will prompt you to switch if you haven’t modified ZSH
So here we see a command prompt that suggests switching to ZSH
Switch command
chsh -s /bin/zsh
Copy the code
How do I check what shells are available on this machine
cat /etc/shells
Copy the code
Configuration files for environment variables
So, let’s look at the environment variable configuration file
What are configuration files for environment variables?
I think the pendulum concept is a little bit confusing, so I think in terms of functions.
To put it more simply, environment variable configuration files are the files that terminal tools load by default when you open terminal tools.
For example, suppose you create a new A file
The a file is loaded by default every time the terminal is opened
The environment variables configured in file A are then loaded into the shell environment
So at this point, you think of the A file as a configuration file for environment variables, why not
Environment variable configuration file
System profile and user profile
There are two types of configuration files, one for the entire system and one for the user
system | The user |
---|---|
/etc/profile | ~/.bashrc |
/etc/bashrc | ~/.zshrc |
— | ~/.bash_profile |
The /etc/directory is the main configuration directory
We can see that the system-level ones are all under /etc/etc
The user configuration is under ~, because CD ~ actually goes to the corresponding user name directory
Load order
Basically load the system first, and then load the user
(There is a complete sequence here, but I have not verified it, so I am afraid to copy and paste it.)
You don’t have to worry about the load order, because normally, we only modify the user’s environment variable file. Conflicts and so on rarely occur in our daily work.
Different variable files in different environments
The following files are user files, that is, ~/.xxx
When we look up various environment variable configurations online, we can see that there are different files that you need to change
Some will let you change.bash_profile, some will let you change.bashrc, some will let you change.zshrc
First, we need to use the echo $SHELL described above to see what the current SHELL environment is
Bashrc is a sibling of bash, usually by modifying either ~/.bash_profile or ~/.bashrc
ZSHRC is a cousin of ZSH, and generally modifies ~/.zshrc files
No files
What?
What will you do without these documents?
Create one manually
cd ~
Create file manually
touch .bash_profile
Copy the code
There you have it
So once it’s created, the terminal is going to read it, you don’t need to do anything else, right
The environment variable
That’s the environment variable configuration file, so let’s talk about environment variables
What are environment variables
To view the current environment variables, run the following command
echo $PATH
Copy the code
The idea is to inject some paths ahead of time so that we can execute some executables quickly so that we don’t have to find a specific path every time.
Maybe some of you don’t understand this very well.
Here are two examples:
Example 1: Configuration of a FLUTTER environment variable
When configuring the Flutter environment, we can see that after git Clone the Flutter project
You can see that there is an executable named flutter under the flutter/bin/ directory
As an extra point, if you download anything that requires environment variables to be configured, there will always be a bin directory, and the executable files will always be in the bin directory. This is a general rule.
Generally, we can determine whether the flutter environment is successfully installed by directly executing the flutter -V at the terminal to see if there is a response
What if we didn’t configure the environment variables and just used the bin/flutter to execute this command?
The answer, of course, is yes.
In other words, after configuration, the effect is:
flutter -v
# is equivalent to
/Users/xing.wu/Documents/environment/flutter/bin/flutter -v
Copy the code
To configure environment variables is to configure the path before the flutter into the default shell path.
The/Users/xing wu/Documents/environment/flutter/bin/configuration to the path
Configuration method
Locate the environment variable configuration file and add it
export PATH=/Users/xing.wu/Documents/environment/flutter/bin:$PATH
Copy the code
In other words, other environment variable configurations can be configured in this way without special commands
export PATH=xxxx:$PATH
Copy the code
After that, you can choose to reopen a new terminal or use the source command to read the environment configuration file
The source command is a built-in shell command for reading and executing commands from a file in the current shell session
Other environment variable files are modified by themselves
source ~/.zshrc
Copy the code
Example 2: NPM global installation package
I’m using an NVM installed Node environment, so the path I add to the path will take NVM
1. First, let’s take a look at which packages I have installed globally
You can see I have a global @vue/cli here
So I can use vue Create Project to create the project
2. So where is the path to the vue
which vue
You can see that the path to/Users/xing wu /. NVM/versions/node/v15.14.0 / bin/vue
Scroll forward to see the result of our echo $PATH
/ Users/xing wu/Documents/environment/flutter/bin: / Users/xing wu /. NVM/versions/node/v15.14.0 / bin: / opt/homebrew/bin: / opt/h omebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/binCopy the code
Can see/Users/xing wu /. NVM/versions/node/v15.14.0 / bin: we joined the environment variable, so can directly use the vue related commands, other libraries in the same way.
We can look at the bin directory
All shortcuts or ontologies to available executables associated with the NPM library are here.
At the end
What is the current SHELL on the Mac, what is the configuration file, and how is the path configured
I believe that the future of the environment configuration is not difficult to fall everyone ~
Here’s a quick question:
ZSHRC is not read by ~. Bash_profile. ZSHRC is not read by ~/.zshrc.
Leave your answers in the comments section