Environmental variables

Time:2020-1-17

Environmental variables
For a long time, the configuration of environment variables is a matter of insight, and the configuration of environment variables is not clear.
Now let’s make up for the deficiencies.

The main contents include:

  • HOME

    • Why do I need $, can’t I print home directly?
    • Is there only one home variable in a system?
    • How to override the home variable defined by the system?
    • Can I access directories directly from $home in Bash?
    • =Is there a space around?
    • How to set the overridden home to be permanent?
  • PATH

    • How to make shell commands in. Bash_profilepermanent in Mac OS
    • The old familiar bin is stored here
  • DIY Variable

    • Set custom variables
    • Read custom variables
    • Sharing custom variables between shells
    • View all shared variables

Consider these questions first:

  • How to set Java, node, go, python bash environment variables?
  • How to set MySQL and mongodb bash environment variables?
  • How to configure the filter image acceleration environment variable pub? Hosted? URL, filter? Storage? Base? URL?
  • What is the difference between bash variable and shell variable?

Understanding of special learning environment variables before:Have a blurred vision
Java, node, go and python will be installed automatically during SDK installation.
MySQL and mongodb bash environment variables need to be added to the environment manually.
The flutter shell environment variable needs to be configured manually.
The most intuitive difference between bash variable and shell variable is that: bash variable is accessed in Bash terminal; shell variable is saved in shell file.

Understanding of special learning environment variables:Very clear-sighted
It is found that the environment variables include so much of the above. The original questions can also be answered one by one.

Next, let’s see what we need to know about environment variables.

HOME

This is a bash var that points to the user’s home directory.
Format is/Users/<username>or/var/root(macOS)。 On Linux system/home/<username>

//Frank user
echo $HOME // /Users/frank
//Root user
echo $HOME // /var/root

Extended questions:

Why do I need $, can’t I print home directly?

In Bash, only after adding $can the system know that we want to output variables, otherwise it will be treated as an output of a normal string.

Is there only one home variable in a system?

Depending on how many users there are on the system, there are several home variables for each user.
Generally, there will be one root user and at least one normal user.

How to override the home variable defined by the system?

Rewrite in ~/.bash profile. (manual source is required to be valid, current session is valid)
In Bash session terminal, export home = / users / Frank / foo. (the current session is valid)
Turn off and turn on or open a new tab, and $home will become the original value.
Rewriting is not recommended. See reason 6 for specific reasons.

Can I access directories directly from $home in Bash?

Sure.$HOME/tools // /Users/frank/tools

=Is there a space around?

No, Spaces have a special meaning in the shell.

How to set the overridden home to be permanent?

/etc/profile //# System-wide .profile for sh(1)
Because a large number of programs depend on the environment variable home, it is dangerous to override the system default variables.
Therefore, it is only valid in the current session. If you want to permanently overwrite it, please refer to the bold part of the path.

PATH

This system variable lists the directory of the executable program.
The reason why Java, node, go and python will be installed automatically during SDK installation is that SDK will automatically add path to path variable.

How to make shell commands in. Bash_profilepermanent in Mac OS

  • Can passmkdir $HOME/binCreate your own bin directory and clickPATH=$PATH:$HOME/binAdd the custom bin directory to the path variable. To write in. Bashrc (Linux),. Bash [profile (MAC OS), use source. Bashrc to make the file take effect immediately. (there is no egg in the pro test. Bash ﹣ profile is only valid in the current session, butThe path is written correctly in the. Bash? Profile)
  • If you want the. Bash? Profile to take effect, you need to set it in terminal’s preferences and start the commandsource ~/.bash_profile

The old familiar bin is stored here

//Bin directories are separated by colons
echo $PATH // /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
  • /Usr / local / bin stores brew, cnpm, docker, docsify, KOA, Mongo, mongod, node, NPM, python3, yarn
  • /Usr / bin stores CD, GIT, Java family buckets, man, python2, SSH, Su, sudo, touch, unzip, VI, VIM
  • /Bin stores MV, rmdir, cat, LN, LS, kill, PWD, echo, MKDIR, RM
  • /There is no common usr / SBIN
  • /sbin ifconfig

Why does / usr / bin contain the bin of Java family bucket?
Isn’t the command of Java family bucket stored in / library / Java / javavirtualmachines / jdk-11.0.3.jdk/contents/home/bin? Why is it in / usr / bin.

adoptls -l /usr/bin | grep javaYou can find out why.

lrwxr-xr-x   1 root   wheel         74  5  4 17:12 java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
lrwxr-xr-x   1 root   wheel         75  5  4 17:12 javac -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/javac
lrwxr-xr-x   1 root   wheel         77  5  4 17:12 javadoc -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/javadoc
lrwxr-xr-x   1 root   wheel         75  5  4 17:12 javah -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/javah
lrwxr-xr-x   1 root   wheel         75  5  4 17:12 javap -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/javap
lrwxr-xr-x   1 root   wheel         82  5  4 17:12 javapackager -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/javapackager
lrwxr-xr-x   1 root   wheel         76  5  4 17:12 javaws -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/javaws

soft link。
/The Java family bucket command in usr / bin is just the soft link of bin file in / library / Java / javavirtualmachines / jdk-11.0.3.jdk/contents/home/bin.

If you don’t understand soft link, you can move to soft and hard links in unxi / Linux

DIY Variable

➜  ~ foo=123
➜  ~ echo $foo
123
➜  ~ bash
bash-3.2$ echo $foo

The bash command in this example will open a daughter process, and the custom variables set in the other process cannot be accessed.

Set custom variables

new_variable="Hello"

Read custom variables

echo $new_variable

Sharing custom variables between shells

How to access custom variables in Bash daughter process, that is, how to share custom variables among processes?

➜  ~ export bar=456
➜  ~ bash
bash-3.2$ echo $bar
456

As you can see from this example, export does this. (export in ES6 is similar to this. Only the content of export can other modules get data.)
Be careful:When the inherited variable value is modified in daughter process, the other process will not be modified.(this is similar to inheritance in programming languages.)

View all shared variables

export -p
...
export USER=frank
export OLDPWD=/
export ZSH=/Users/frank/.oh-my-zsh
export bar=456
...

The oldpwd variable refers to the last directory accessed through thecd -This is the directory you are accessing.
Again, throughenvYou can also get a list of all environment variables directly. The difference is that it does not have export and is the final result of environment variables,export -pThe detailed configuration of the variable is displayed (because it may depend on other environment variables).

Reflection and summary

  • Mainly in-depth understanding of home, path and DIY environment variables
  • Have a clear understanding of the familiar bin,. Bash_profile, export, etc
  • According to the reference bash variables environmental and other wise, I have done some thinking and practice in Mac OS environment
  • There are no environment variables at the language level, such as node ﹣ Env, process.env, etc., but if you master the above content, it will be easier to understand these environment variables

Recommended Today

RCAST 35: add type to currency

– font ALT: Simsun; MSO font charset: 134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 680460288 22 0 262145 0;} @font-face {font-family:”Cambria Math”; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:1; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-family:Calibri; Variable; Ose-1: 216301111; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 680460288 22 0 262145 0;} /\* Style […]