Organize from
Https://www.shiyanlou.com/courses/running/60
To explain the environment variables, you have to understand what the variables are, and exactly what they should be, the so-called variables are symbols that are used in a computer to record a value (not necessarily a numeric or a character or a string), and these symbols will be used in different arithmetic processing. Usually a variable is a one-to-many relationship with a value, you can assign a value to another variable by reading its value, or you can assign a value to any variable directly. For ease of operation and processing, most programming languages distinguish between the types of variables used to record data types, such as numeric values, characters, or strings. The variables in the Shell are basically the same, there are different types (but not specifically specifying the type name), you can participate in the operation, scoped.
How to create a variable in the Shell, how to assign a value to a variable, and how to read the value of a variable? This section is described in detail in the Bash scripting lesson, where I'll give you a quick example:
Use declare
the command to create a variable named tmp:
declare tmp
In fact, you can also use the declare to pre-declare a variable, which is created directly, just to tell you the role of declare, which is used when creating other variables of the specified type (such as an array).
Use =
the number assignment operator to assign a value of Shiyanlou to the variable tmp:
$ tmp=shiyanlou
Read the value of a variable, using echo
commands and $
symbols (the $ symbol is used to denote a value that refers to a variable, and beginners often forget to enter it):
echo $tmp
2. Environment variables
Simple understanding of the concept of variables, it is very good to explain the environment variables, environment variables are scoped than the custom variable, such as the shell environment variables acting on itself and its child process. In all Unix and Unix-like systems, each process has its own set of environment variables, and by default, when a process is created, it is explicitly specified in the process creation, and it inherits most of the environment settings of its parent process. The shell program also runs on the operating system as a process, and most of the commands we run in the shell will run as a child process of the shell.
There are three types of environment variables that we will typically relate to:
- The current shell process private user-defined variables, such as the TEMP variable we created above, are only valid in the current shell.
- The built-in variables of the Shell itself.
- An environment variable exported from a custom variable.
There are also three commands related to the above three environment variables,, set
, env
export
. These three commands are similar and can be used to print related environment variables, except that they are related to different ranges of environment variables, as described in the following table:
Command |
Description |
set |
Displays all environment variables for the current shell, including built-in environment variables (related to shell appearance, etc.), user-defined variables, and exported environment variables |
env |
Displays the environment variables that are relevant to the current user, and lets the command run in the specified environment |
export |
Displays variables that are exported from the Shell to an environment variable, and can also be exported to a custom variable as an environment variable |
You can use the tools more intuitively to vimdiff
compare the differences between them:
$ Temp=shiyanlou
$ Export Temp_env=shiyanlou
$ env|sort>env.txt
$ export|sort>export.txt
$ set|sort>set.txt
The above operation sorts the command output by using a pipe |
sort
command and then redirects to the object text file.
export.txt set.txt
As for environment variables, it can be easily understood that the child processes in the current process are valid and valid as environment variables, otherwise (some people also refer to all variables collectively as environment variables, just to differentiate between global environment variables and local environment variables, so long as we understand their real differences). Let's take export
a look at the command here, first set a variable in the shell temp=shiyanlou
, and then create a new child shell to see temp
the value of the variable:
3. Search path and order of commands
You might have had a long question about it, and we entered a command in the shell, how did the shell know where to find the command and execute it? This is done through environment variables PATH
, and users who are familiar with Windows may know that there is a PATH environment variable in Windows. This PATH
saves the search path to the command executed in the shell.
PATH
to view the contents of an environment variable:
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
If you remember what we did in the Linux directory structure section, you should know what kind of files are being relegated to these directories. Usually this kind of directory is the executable file, when we execute a command in the Shell, the system will follow the path set in the order of the paths to the directory to find, if there is a command with the same name, then the first to find the one.
Below we will practice creating one of the simplest executable Shell scripts and a "Hello World" program created using the C language, if you have not studied these two parts before, then you can do an introductory study: C Language Primer Advanced Bash Scripting Programming Guide Linux Shel L Scripting Tutorial (LSST) v2.0
Create a Shell script file:
$ vim hello_shell.sh
Add the following in the script, save and exit ( Note that the first line is not omitted, this is not a comment, the forum has a user response to a syntax error, because there is no first line ):
#!/bin/zsh
For ((i=0; i<; i++));
Do echo "Hello Shell"
Done
Exit
chmod 755 hello_shell.sh
Create a C language "Hello World" program:
$ vim hello_world.c
#include <stdio.h>int main(void){ printf("hello world!\n"); return 0;}
To generate an executable file using gcc:
$ gcc -o hello_world hello_world.c
GCC-generated binaries have executable permissions by default and do not need to be modified
Create a directory in the Shiyanlou home directory mybin
and move the above hello_shell.sh and Hello_world files to it:
$ mkdir mybin$ mv hello_shell.sh hello_world mybin/
Now you can mybin
run each of the two programs you just created in the directory:
$ cd mybin$ ./hello_shell.sh$ ./hello_world
Back to the top level of the directory, that is, the shiyanlou
home directory, when you want to run the two programs, you will find the prompt command can not be found, unless the full path of the command, but it is inconvenient, how do you want to use the same system command to execute the script file or program you created? Then add the path of the command to the PATH
environment variable.
4. Add a custom path to the "path" environment variable
In the front we should notice that PATH
the path inside is :
as a delimiter, so we can add a custom path like this:
$ PATH=$PATH:/home/shiyanlou/mybin
Note that absolute paths must be used here
Now you can execute those two commands in any other directory. You may realize that this is not a good solution, because I have added a path to the PATH environment variable, it is only valid in the current Shell, once I exit the terminal, and then open it will be found to fail. Is there a way to make the added environment variable globally valid? Or do you automatically execute the commands above to add a custom path to path each time you start the Shell? So let's talk about the latter way--let it execute automatically.
In each user's home directory, there is a Shell that executes a configuration script by default each time it starts to initialize the environment, including adding some user-defined environment variables, and so on. Zsh profile is, the .zshrc
corresponding Bash configuration file is .bashrc
. They etc
also have one or more global profiles, but generally we only modify the configuration files in the user directory.
We can simply use the following command to add content directly to the .zshrc
:
$ echo "PATH=$PATH:/home/shiyanlou/mybin" >> .zshrc
The above command >>
indicates that the standard output is redirected to a file in an append manner, noting that the previous use is to redirect to >
a file in a way that is overwritten and must be distinguished when used. A new file will be created if the specified file does not exist.
5. Modify and delete existing variables
Variable modification
There are several ways to modify variables:
How variables are set |
Description |
${变量名#匹配字串} |
Start the match from the back to delete the shortest data matching the matched string |
${变量名##匹配字串} |
Start the match from the back to delete the longest data matching the matched string |
${变量名%匹配字串} |
Match from tail forward to delete the shortest data matching string |
${变量名%%匹配字串} |
Match from tail forward to delete maximum data matching string |
${变量名/旧的字串/新的字串} |
Replace the first string that matches the old string with a new string |
${变量名//旧的字串/新的字串} |
Replace all strings that match the old string with a new string |
For example, modify the environment variables we added to PATH earlier. In order to avoid errors that result in the command not being found, we first assign the path to a new custom variable path:
$ path=$PATH
$ echo $path
$ path=${path%/home/shiyanlou/mybin}
# or use wildcards, * denotes any number of characters
$ path=${path%*/mybin}
Variable deletion
You can use the unset
command to delete an environment variable:
unset temp
6. How to make environment variables effective immediately
After we have modified a configuration script file in the Shell (for example, in the home directory of the zsh configuration file .zshrc
), we have to exit the terminal to reopen or even restart the host computer before it can take effect, it is troublesome, we can use the source
command to let it take effect immediately, such as:
source .zshrc
source
The command also has an alias, which is to note that the .
Point area that represents the current path is separate, albeit in the same form, but in the same way that the above command, if replaced, .
is
$ . ./.zshrc
Note that the first point is followed by a space, and the subsequent file must specify the complete absolute or relative pathname, which is not required by source.
Second, search documents
Search-related commands are commonly used in the following sections,,, whereis
which
find
locate
.
whereis
Simple and fast$whereis who
You'll see that it finds three paths, two executable paths, and a path to a man's online Help file, and this search is quick because it's not looking from the hard drive, it's querying directly from the database. whereis
only Binary (-B), man Help file (-m), and source code file (-s) can be searched. You can use the command if you want to get a more comprehensive search result locate
.
Through the "/var/lib/mlocate/mlocate.db" Database lookup, but this database is not updated in real-time, the system will use scheduled tasks to automatically perform the updatedb
command update once a day, so sometimes you just add the file, it may not be found, you need to do it manually updatedb
command (This command must be executed first in our environment). It can be used to find different file types under the specified directory, such as finding all files that start with SH under/etc:
$ locate /etc/sh
Note that it is not just looking in the ETC directory and will automatically recursively look up subdirectories
Find all JPG files under/usr/share/:
/usr/share/\*.jpg
Notice that you want to escape the backslash *
before adding the number, otherwise you will not find
If you want only the number of statistics can be added -c
parameters, -i
parameters can be ignored case to find, Whereis, the -b
-m
-s
same can be used.
which
itself is a shell built-in command that we typically use which
to determine if a specified software is installed because it only PATH
searches for the command from the path specified by the environment variable:
which man
find
Should be the most powerful of these commands, not only can be found by file type, file name and can be based on the properties of the file (such as the file's timestamp, file permissions, etc.) to search. The find
command is strong, to make it clear that you need at least a few separate lessons, and we'll just cover some of the things we use most often.
Search for files of the specified file name under the specified directory:
/etc/ -name interfaces
Note the path to the Find command is the first parameter, and the basic command format is find [path] [option] [action]
Time-related command parameters:
Parameters |
Description |
-atime |
Last Access time |
-ctime |
Creation time |
-mtime |
Last Modified Time |
List files in the home directory that have changed in the day (within 24 hours):
$ find ~ -mtime 0
List the new files in the user home directory than the documents/test.c~ file:
$ find ~ -newer Documents/test.c\~
Linux environment variables and file lookups