Deleting environment variables in linux system does not take effect|How to delete environment variables in linux

0 Preface

Today, when writing a shell script to read the environment variable, I found that the cancellation of the environment variable has not taken effect. Record the solution for future reference.

1. Questions

First the script is as follows:

Get the environment variable JAVA_HEAP_XMXand JAVA_HEAP_XMNfinally print it out, if the environment variable is empty, print the default value

#!/usr/bin/env bash

# 指定堆内存
HEAP_XMX="2g"
HEAP_XMN="512m"

if [ -n "$JAVA_HEAP_XMX" ]; then
    HEAP_XMX=${JAVA_HEAP_XMX}
fi

if [ -n "$JAVA_HEAP_XMN" ]; then
    HEAP_XMN=${JAVA_HEAP_XMN}
fi

echo "堆内存大小: ${HEAP_XMX}"
echo "堆内存年轻代大小: ${HEAP_XMN}"

/etc/profileEnvironment variables are configured in the file :

export JAVA_HEAP_XMX=3g
export JAVA_HEAP_XMN=1g

After source /etc/profilereloading the environment variable, execute the script, and find out that the acquisition was successful through the output.

insert image description here

But when we cancel the environment variable

insert image description here
We found that the previous environment variable value was still loaded, which means that the environment variable we deleted did not take effect
insert image description here

Why is this?

2. Solve

In fact, it goes back to sourcethe loading principle of the instruction. The source loads the environment variables configured in the file into the cache, but because the environment variables are deleted, the source is actually not obtained when it is obtained, and the cache will not be updated. .

So if we want to delete environment variables separately, we also need to use unsetinstructions

unset JAVA_HEAP_XMX
unset JAVA_HEAP_XMN

insert image description here

After execution, our environment variable is canceled from the cache, and the above script prints the default value

3. Summary

To cancel the environment variable, in addition to modifying /etc/profilethe file, unset 环境变量名delete the environment variable cache by

Guess you like

Origin blog.csdn.net/qq_24950043/article/details/130516319