variable
- Variable is computer for recording a value (not necessarily the value may be a character or character string) of symbols, and the symbols for various arithmetic processing.
- Variables are generally one to one relationship value, its value may be read by the expression and assigned to the other variables can be specified directly assigned to any variable value.
- For ease of calculation and processing, most of programming language distinguishes the type of the variable for each record number, characters or strings of data types and the like. Shell variables in basically the same, there are different types (but not specifically designated type name), can participate in operations, there is limited scope.
I.e., the effective range of the variable scope of the variable (such as a function in a source file or global scope), within the range of only one variable of the same name. Upon exiting the valid variable, this variable does not exist as a general.
How to create a variable in Shell, how to assign values to variables and how to read the value of the variable it? This section will be in bash scripting details of this course, a simple example to illustrate this:
Use declare
command to create a variable named tmp variable:
$ declare tmp
In fact, you can not declare a variable pre-declaration, direct use namely creation, here just telling you declare the action, which will be used when creating other specified types of variables (such as arrays).
Use =
number assignment operator, assigned to the variable tmp shiyanlou:
$ tmp=shiyanlou
Read the value of a variable, use the echo
command and $
symbol ( $ symbol used to represent the reference value of a variable, beginners often forget to enter ):
$ echo $tmp
Note: Not all forms of variable names are available, the variable name can only be letters, numbers or underscores, and can not be used as a digital beginning.
1. Environment Variables
- Environment Variables scope than the custom variable to be large, such as Shell environment variables acting on itself and its children .
- In all UNIX and UNIX-like systems, each process has its own environment variable settings, and by default, when a process is created, in addition to the creation process explicitly specified, it will inherit its parent process must most of environment settings. Shell program also runs as a process on top of the operating system, and the way we run most commands in the Shell Shell will have the child process will run.
Variable types There are three:
- Current Shell process private tmp variable user-defined variables, such as the above, we create only valid in the current Shell.
- Shell itself is built-in variables.
- Custom variables derived from environment variables.
There are also three commands related to the three environment variables: set
, env
, export
. These three commands are similar, are used to print variable information environment , except that a range of different variables involved. The table below:
command | Explanation |
---|---|
set |
Displays the current Shell all variables, including the built-in environment variables (related to appearance Shell), user-defined variables and environment variables derived. |
env |
Display environment variables associated with the current user, also allows the command to run in the specified environment. |
export |
Derived from Shell to display the variable environment variables, it can also be exported as environment variables custom variable. |
More intuitive to use vimdiff
tool to 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 command output through the conduit |
using the sort
command queuing, and then redirected to the object in a text file.
$ vimdiff env.txt export.txt set.txt
About which variables are environment variables , can be simply understood as the child of the current process effective compared with environmental variables, or not (some people have all the variables collectively referred to as environment variables, but to the global environment variables and local environment variables to distinguish, As long as we can understand their real difference). We are here with a export
command to taste, to set a variable in the Shell temp=shiyanlou
, and then create a new sub Shell to view temp
variable values:
Note: In order to distinguish from ordinary variable, usually we are used to set the environment variable name in uppercase.
Permanent
But the question is, when shut down or close the current shell, the environment variable is gone. How to make it permanent environmental variables?
Press the lifetime of variables to divide, Linux variables can be divided into two categories:
- Permanent: the need to modify the configuration file, variable permanent;
- Temporary: Use the command-line statement to export, variable fail when you close the shell.
Here are two important documents /etc/bashrc
(some Linux do not have this document) and /etc/profile
they were stored in a shell and environment variables . Also to be noted is the difference between a hidden file in the directory for each user:
.profile 可以用 ls -a 查看
cd /home/shiyanlou
ls -a
The .profile only permanent effect on the current user. Written in /etc/profile
there is permanent for all users, so if you want to add a permanent environment variable, just open /etc/profile
, add the final environment variable you want to add a thousand million.
2. Command search path and order
You may long ago have doubts, we enter a command in the Shell, Shell is how to know where to find the commands and then execute it? This is done by the environment variable PATH
to search the familiar Windows users may know that Windows is also in such a PATH environment variable. The PATH
inside path is saved search Shell command executed.
Check PATH
the contents of the environment variable:
$ echo $PATH
By default, you will see the following output:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
Usually this kind of decentralization directory is an executable file that when we execute a command in the Shell, the system will follow the path set PATH accordance with the order to the directory to find, if there is a command of the same name, is executed first find one.
Now we will practice creating a simple executable Shell scripts and the use of a "hello world" program in C language created.
Create a Shell script file:
$ cd /home/shiyanlou
$ touch hello_shell.sh
$ gedit hello_shell.sh
Add the following in the script, save and exit (be careful not to omit the first line, this is not a comment):
#!/bin/bash
for ((i=0; i<10; i++));do
echo "hello shell"
done
exit 0
Add executable permissions for the file:
$ chmod 755 hello_shell.sh
Execute the script:
$ cd /home/shiyanlou
$ ./hello_shell.sh
C language to create a "hello world" program:
$ cd /home/shiyanlou
$ gedit hello_world.c
#include <stdio.h>
int main(void)
{
printf("hello world!\n");
return 0;
}
After saving an executable file using gcc:
$ gcc -o hello_world hello_world.c
gcc generates a binary executable file has the default permissions, no need to modify
Create an in / home / shiyanlou home directory mybin
directory, and moving the hello_shell.sh and hello_world files to it:
$ cd /home/shiyanlou
$ mkdir mybin
$ mv hello_shell.sh hello_world mybin/
Now you can mybin
run two programs are you just created directory:
$ cd mybin
$ ./hello_shell.sh
$ ./hello_world
Back to the parent directory, which is the shiyanlou
home directory, run think when those two programs, you will find the command prompt can not find, unless coupled with the full path of the command, but that is very convenient, how do like to use the system execute the command as a script file or program that you create it? Then the path where you want to add commands to the PATH
environment variables.
3. Add the custom path to "PATH" environment variable
We should note in front of PATH
the inside of the path is :
a delimiter, so we can add custom path:
$ PATH=$PATH:/home/shiyanlou/mybin
Note that there must be an absolute path.
Now you can execute two commands in any directory of the (attention to the need to remove the front ./
). You might also realize that this is not a good solution to the problem, because I PATH environment variable to append a path, it is only valid in the current Shell, once I exit the terminal, then open and you will find ineffective. There is no way for the global environment variable to add effective? Or performed automatically each time adding a custom path to the PATH command above when starting Shell? Here we are concerned that the latter approach - it automatically.
There is a Shell will each perform a default configuration script at startup in each user's home directory to initialize the environment, including user-defined add some environment variables, and so on. zsh configuration file is .zshrc
corresponding to Bash profile .bashrc
. They etc
also have one or more global configuration file, but we generally only modify the configuration files in the user directory.
We can simply use the following command to add content directly .zshrc
in:
$ echo "PATH=$PATH:/home/shiyanlou/mybin" >> .zshrc
>> command above shows a standard manner additional output redirected to a file, note that used earlier> cover is redirected to a file, must pay attention to the use of time resolution. It creates a new file if the specified file does not exist.
4. modify, and delete the existing variable
Modify variables
Modify variables in the following ways:
Variable arrangement | Explanation |
---|---|
${变量名#匹配字串} |
Start back from scratch match, delete data in line with the shortest matching string |
${变量名##匹配字串} |
Start back from scratch match, delete the line with the longest matching string of data |
${变量名%匹配字串} |
Matches start from the tail forward, delete data in line with the shortest matching string |
${变量名%%匹配字串} |
Matches start from the tail forward, delete the line with the longest matching string of data |
${变量名/旧的字串/新的字串} |
The first string in line with the old string is replaced with a new string |
${变量名//旧的字串/新的字串} |
All in line with the old string strings replaced with a new string |
To modify our previous example to add to the PATH environment variable. In order to avoid operational errors led to a command is not found, we assign a new custom variable path first PATH:
$ path=$PATH
$ echo $path
$ path=${path%/home/shiyanlou/mybin}
# 或使用通配符,*表示任意多个任意字符
$ path=${path%*/mybin}
Variables deleted
You can use unset
the command to delete an environment variable:
$ unset temp
5. How to make the environment variables to take effect immediately
After we modified the front of a configuration script file in the Shell (zsh such as lower profile home directory .zshrc
), every time even exit the terminal reopened after its reboot the host to take effect, it is troublesome, we can use the source
command to make its effect immediately, such as:
$ cd /home/shiyanlou
$ source .zshrc
source
There is an alias command .
, if the above command replace .
way would be:
$ . ./.zshrc
In use .
, when the need to pay attention to the point that indicates the current separate area of the path.
Note that the first point followed by a space, and the back of the document must specify the full absolute or relative path name, source is not required.
Learning Resources from laboratory building site https://www.shiyanlou.com/courses/1/learning/?id=60