content
Shell Environment Variables In-Depth: Loading Process Testing
Shell login environment execution script file syntax
Shell non-login environment variable execution script file syntax
Shell Environment Variables In-Depth: Identifying Shell Environment Types
Shell environment variables in-depth: switch the shell environment in detail
Shell Environment Variables In-Depth: Loading Process Testing
Target
Understand the loading process test of Shell environment variables
Ability to know where environment variables should be configured
Introduction to Switching Shell Environments to Execute Script Files
When executing a script file, you can specify a specific shell environment to execute the script file. This is to switch the shell environment to execute the script.
Shell login environment execution script file syntax
sh/bash -l/--login 脚本文件
Meaning: First load the Shell login environment process to initialize the environment variables, and then execute the script file
Shell non-login environment variable execution script file syntax
bash # 加载Shell非登录环境
sh/bash 脚本文件 # 直接执行脚本文件
Meaning: First execute the process of loading the Shell non-login environment to initialize the environment variables, and then execute the script file
Test Case
need
The shell login environment will run /etc/profile
Shell non-login environment will run /.bashrc
Set the environment variable data in the /etc/profile and /current user/.bashrc files respectively, then output the environment variable data in the shell script file, and finally switch to different environments to execute the shell script file to observe and verify the operation of the above process
analyze
-
Clean up, clean up the VAR1 environment variable in the /etc/profile file to delete, and reload the configuration file
-
Edit /etc/profile, add environment variable VAR1=VAR1
-
Edit /root/.bashrc, add environment variable VAR2=VAR2
-
Create the demo1.sh file, read the environment variable data for printing
# output environment variable VAR1 # output environment variable VAR2
-
Execute the demo1.sh script file in a shell non-login environment, and observe that only VAR2 is output, not VAR1
-
Execute the demo1.sh script file in the shell login environment, and observe that VAR2 and VAR1 will be output
demo
Edit /etc/profile file
gedit /etc/profile
Edit and add the following content, save and exit
In the root directory, edit the .bashrc file
gedit .bashrc
Edit and add the last 2 lines as follows, save and exit
Create the file demo1.sh
touch demo1.sh
Edit the file demo1.sh and add the following content
#!/bin/bash
echo $VAR1
echo $VAR2
Execute the script file directly
bash demo1.sh
Execute the script file directly, that is, neither the login shell environment variables nor the non-login shell environment variables are loaded
First load the non-login shell environment variables, and then execute the script file
bash
bash demo1.sh
Shell non-login environment will load
当前用户/.bashrc
theSo here will output the environment variable data of VAR2
First load the login shell environment variable, and then execute the script file
bash -l demo1.sh
The shell login environment
etc/profile
loads当前用户/.bashrc
the file and environment variable data forSo here will output the environment variable data of VAR1 and VAR2
summary
1. The initial loading principle process of Shell environment variables
Classification | Initialize Environment Variables Process Execution File Order |
---|---|
Shell login environment initialization process | /etc/profile--》/etc/profile.d/*.sh--》~/.bash_profile--》~/.bashrc--》/etc/bashrc |
Shell non-login environment initialization process | ~/.bashrc--》/etc/bashrc--》/etc/profile.d/*.sh |
2. So where are the environment variables defined? Difference between /etc/profile and /etc/bashrc?
The environment variables read by the executed shell script that needs to log in are configured in: /etc/profile, /current user/.bash_profile
The environment variables read by shell scripts executed by users who do not need to log in are configured in: /current user/.bashrc, /etc/bashrc
Shell Environment Variables In-Depth: Identifying Shell Environment Types
Target
Understand how to identify shell login environments and non-login environments
grammar
Use $0 to recognize ambient syntax
echo $0
Output
-bash
stands for : shell login environmentoutput
bash
stands for : shell non-login environmentNote: This
$0
environment variable if used in a subshell (shell script file) outputs the filename of the shell script itself
bash command syntax
bash
Bash command: used to switch to Shell non-login environment
analyze
1. Log in directly to the system and output $0 for the shell login environment to observe the effect of the output information
2. Use the bash command to switch to the shell non-login environment and output $0 to observe the effect of the output information
3. Create the test.sh script file, edit and add the output $0, and execute the test.sh script file after the programming is saved to observe the effect of the output information
summary
1. How to identify the shell login environment and non-login environment?
$0 用于获取当前Shell环境的类型, bash代表Shell非登录环境, -bash 代表Shell登录环境
# $0不可以在脚本文件中使用, 因为代表获取脚本文件名字
Shell environment variables in-depth: switch the shell environment in detail
Target
Understand commands to switch shell environments
Introduction to switching shell environment commands
-
Direct login to load the shell login environment
-
su switches user to load Shell login and Shell non-login environment
-
Bash loads Shell login and Shell non-login environment
Switch environment mode: bash switch
Order
Syntax 1:
bash # 加载【Shell非登录环境】
Syntax 2:
bash -l Shell脚本文件 / bash --login shell脚本文件
sh -l shell脚本文件 / sh --login shell脚本文件
# 先加载【Shell登录环境】然后运行指定Shell脚本文件
analyze
Use bash to execute the test.sh script file, and an error occurs indicating that the current environment is the shell non-login environment