Shell Script Raiders (learning note)--1.4 variable (base)

Source: Internet
Author: User

Suppose that the variable str, when setting or modifying a variable property, does not have a $ number, only a reference variable is used. This means that the variable is str, not $str. $ is just a symbolic form of manipulating variables, as well as symbolic forms such as ${}, ${#}, and $ (()), which represent different meanings, respectively. This is easy to mistake.

1.4.1 Environment Variables

Environment variables are run in the environment context and can be referenced in this context. For example, the common CD, LS and other commands strictly should use absolute path such as/bin/ls to execute, precisely because of the/bin directory added to the PATH environment variable, the system itself will go to find the path under paths have the command.

Environment variables are usually capitalized. Common environment variables are hostname, SHELL, histsize, USER, PATH, PWD, LANG, HOME, LOGNAME. Each indicates the current hostname, the path of the shell, the type of bash, how many records the history holds, the current user name, the automatic search path, the current directory, the language used (the variable is changed when the language is temporarily modified), the current user's home directory, and the currently logged-on user.

Use env to view the current user's environment variables.

echo $PATH  #查看PATH环境变量

/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

1.4.2 Common Variables

In scripting languages, variables are usually not specifically declared and can be assigned directly to a value. In bash, variables are stored by default as string types, regardless of whether they are used in quotation marks or not.

Variable assignment: Str=value, there are no spaces around the equals sign. If there is a space, the comparison operator is the comparison operation.

Variable reference method: $str or ${str}, some places must use ${STR} reference way , for example, in double quotes echo "The Var is ${str}".

[Email protected] tmp]# str='Hello world! '  echo"We'll say ${str}"

We'll say Hello world! #注意到没, the exclamation point unexpectedly output. So special symbols are not sometimes incorrectly identified in variables like echo.

Release variable: unset str #没加前缀 $.

[Email protected] ~]# unset str

View all temporary variables: Set or DECLARE commands that do not receive parameters.

Define read-only variable: readonly str. You will not be able to modify the variable value or unset the variable, and you can only log back in to delete the variable.

Temporarily upgrade normal variables to environment variables: Export str or assign export str= "value", so that $str can be used in the current shell and child shell, but exit the script or re-login will cancel the export effect.

[Email protected] ~]# unset str;str='Hello world! '; Echo $str

Hello world!

[Email protected] ~]# bash   echo ${str} #子shell中查看变量结果发现没有该变量.

Looking at the variable in the child shell, the result is that there is no such variable. This is because the scope of the $STR is only in the current shell, and if you want to refer to a normal variable in a child shell, you need to use export to upgrade to an environment variable.

[Email protected] ~]# exit   #退出子shell

Exit

[[Email protected] ~~echo $str

Hello world!

1.4.3 Modifying the life cycle and scope of variables

Normal variables expire at the end of the script or log out, and are valid only for the current shell, and are not available to other users and the child shell of the current user.

Using export can be promoted to a temporary local environment variable, valid for the current user's current shell and child shell, but other users are invalid, exiting the script and exiting the login also fails.

Writing a variable to a /etc/profile file allows the variable to become permanent and global. Not only variables, other settings are written to the same file. This file is a file that is called before each user logs on, so it is permanently valid for all users. A restart is required after modifying the file, or the source/etc/profile is used to re-invoke it to take effect.

Writes the variable to the corresponding user's home directory. Bash_profile that is, the ~/.bash_profile file can be permanent but only valid for the corresponding user. Not only variables, but other settings as well. This file is a file that is read by each user after login, so it is valid only for the corresponding user, and of course the corresponding child shell. It is also necessary to restart or use the source ~/.bash_profile to make the call to take effect.

The read and invoke process for bash is briefly described here:

Login Linuxè Read/etc/profile--> read the corresponding user's. bash_profile or. Bash_login or. Profile (priority is reduced from front to back, so there are three kinds of files that are reserved for different Linux transitions)-- The. BASHRC for the user (because the. Bash_profile is called so that after execution, the child shell reads )-->/etc/bashrc-->.bash_logout (some of the actions that are performed when exiting, Complete exit only after execution is completed).

If there are. sh files in the/etc/profile.d/directory, they will be called when the/etc/profile is read, and they will be called when the/ETC/BASHRC is read.

1.4.4 Gets the length of the variable

When you reference a variable by using the ${} method, the variable name is preceded by a # to see the character length of the variable. Spaces are also counted in length. For example:

echo ${#str}

12

echo ${#PATH}

92

1.4.5 Declare declaring variables

Declare [+/-][option] Variable name

Options:

-/+: Sets the type attribute to the variable, cancels the type attribute set by the variable

-I: Declared As Integer

-x: Declared environment variable

-P: Explicitly specifying the type of the variable being declared

For example, declare an environment variable Declare-x str, and cancel the variable declare +x str.

Shell Script Raiders (learning note)--1.4 variable (base)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.