Environment variables
Variables: Shell variables, which are symbols used to record a value in a computer, are used in different operations. Usually a variable is a one-to-many relationship, you can assign a value to another variable by reading its value from an expression, or you can assign a value to any variable directly. The scope of a variable is a finite range of variables, where there can be only one variable with the same name, and the variable is invalid once it is left.
Create a variable named tmp in the Shell $declare tmp (just pre-declare a variable), or you can assign a value to the variable tmp=forever (there can be no spaces in the middle). Read the value of the variable, use the Echo command and the $ symbol ($ symbol to represent the value that refers to a variable) $echo $tmp. Variable names can only be English letters, numbers, or underscores, and cannot begin with a number.
Environment variables: Scopes are larger than custom variables, such as the shell's environment variable scope itself and its child processes, in all Unix and Unix-like systems, each process has its child environment variable settings, and by default, when a process is created, the process is explicitly specified during creation, It will inherit most of the environment settings of the parent process. Shell programs also run as a process on the operating system, and most of the commands we run in the shell will run as child processes of the shell.
There are three types of environment variables that we will typically relate to: the current shell process private user-defined variables, only valid in the current shell, the shell itself built-in variables, and environment variables exported from custom variables. The commands related to the above three environment variables are set,env,export respectively. In order to differentiate from ordinary variables, the environment variable name is often capitalized.
Set: Displays all environment variables for the current shell, including built-in environment variables, user-defined variables, and exported environment variables
ENV: Displays the environment variables associated with the current user and allows the command to run in the specified environment
Export: Displays a variable that exports an environment variable from the shell, and can also export the custom variable as an environment variable
$ Temp=shiyanlou
$ Export Temp_env=shiyanlou
$ env | Sort>env.txt
$ export | Sort>export.txt
$ set | Sort>set.txt
$ vimdiff env.txt export.txt set.txt//using Vimdiff to compare the contents of three export files
$ Temp=shiyanlou
$ echo Temp
$ zsh//Create child shell
$ echo $temp
$ exit
$ Export Temp
$ zsh
$ echo $temp
Command lookup path and order: the shell is searched through the environment variable path. $ echo $PATH (view the contents of the environment variable)
$vim hello_shell.sh
#!/bin/zsh
for ((i=0; i<10;i++));d o
echo "Hello Shell"
Done
Exit 0//Edit in Vim
$ chmod 755 hello_shell.sh//Add executable permissions for files
$ vim HELLO_WORLD.C
#include <stdio.h>
int main (void) {
printf ("Hello world!\n");
return 0;
}
$ gcc-o Hello_world hello_world.c//using GCC to generate executable files
$ mkdir Mybin
$ mv hello_shell.sh Hello_world mybin/
$ CD Mybin
$./hello_shell.sh
$./hello_world
Add a custom path to the PATH environment variable
A path path is: As a delimiter.
$ path= $PATH:/home/shiyaolou/mybin//The absolute path must be used here, but when you exit the terminal, the added environment variable is invalidated.
In each user's home directory, there is a shell that executes a configuration script to initialize the environment, including adding some user-defined environment variables, for each boot. The configuration file for zsh is. ZSHRC, and the corresponding bash configuration file is. bashrc. They also have one or more global profiles under the ECT, but they generally refer to modifying the configuration file under the user directory.
$ echo "path= $PATH:/home/shiyanlou/mybin" >>.ZSHRC//>> indicates that the standard output is redirected to a file in an append manner, and if > is redirected to a file as overridden.
Modify and delete an existing variable
There are several ways to modify the variables:
${Variable name # Match string} Start from the beginning to match and delete the shortest data that matches the matching string
${variable name # #匹配字串} start the match from the beginning and delete the longest data matching the matched string
${variable name% matching string} start matching from tail forward, delete shortest data matching string
${variable name percent-matched string} to match from tail forward, deleting the longest data matching the matched string
${variable name/old string/new string} will be in line with the first of the strings replaced by the new word string
${variable name//old string/new string} Replace all strings of the matching string with a new string
Variable Delete $ unset temp
After we have modified a configuration script file in the shell, it will take effect every time we exit the terminal to reopen or even reboot the host, but we can use the source command to make it effective immediately. $ source. ZSHRC The source command also has an individual name. $ . ./.ZSHRC (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).
Search for files
Search-related commands are commonly used: whereis,which,find,locate.
$ whereis WHO//simple fast This search is fast, and is not looked up from the hard disk, but directly from the database. Whereis can only search binaries (-B), man Help Files (-m), and source code files (-s).
$ locate/etc/sh//fast and full through the "/var/lib/mlocate/mlocate.db" Database lookup. However, this database is not updated in real time, and the system automatically executes the UpdateDB command every day using a timed task. So sometimes the files you just added will not be found and you need to manually execute the updatedb command. It can be used to find different file types under the specified directory.
$ locate/usr/share/\*.jpg//find/usr/share/all jpg files * preceding \ means escaping if you want to only count the number can be added to the-c parameter,-I can ignore the case to find. Whereis's-b,-m,-s also tried.
The which itself is a shell built-in command that typically uses which to determine whether a specified software is installed because it searches for commands only from the path specified in the PATH variable.
$ which man//fine and fine
Find not only can be found by file type, filename, but also based on the properties of the file.
$ find/etc/-name Interfaces//find command path is the first parameter, the basic command format is find[path][option][action]
Time-related command parameters:
-atime Last access time-ctime creation time-mtime last modified time
-mtime N # is a number that represents a modified file "within one day" before N days
-mtime +n: Lists files that have been modified before n days (not including the N-day itself)
-mtime-n: Lists files that have been modified before n days (containing the N-day itself)
Newer File:file is a file that already exists, listing a new filename
$ find ~ mtime 0//List files in home directory that have changed within 24 hours of the day
$ find ~ Newer documents/test.c\~//list new files in user home directory than document/test.c~ files
Digital Rain
$ sudo apt-get update; sudo apt-get install Cmatrix
Environment variables and file lookups