Shell programming in the 3rd Chapter Shell Variable Foundation (PART I)

Source: Internet
Author: User
Tags define local exit in

1. What is a shell variable
Simply put, a variable is a fixed string instead of more complex content, which may also contain variables, paths, strings, and other content. Variables are places where data is stored temporarily and data tags, the stored data exists in the memory space, the data corresponding to the variable can be fetched by correctly calling the name of the variable in memory space.
Significance: The greatest advantage of using variables is to make program development more convenient, of course, the use of variables in programming is also necessary, otherwise it is difficult to complete the related program development work.
2. How to use Variables (Assignment)
[Email protected] ~]# oldboy= "I am Oldboy"
[Email protected] ~]# echo $oldboy
I am Oldboy
The contents of the variable are generally enclosed in double quotes to prevent errors, especially if there are spaces between the contents of the value.
3. Variable type
Variables can be divided into two categories: environment variables (also called global variables) and ordinary variables (also called local variables).
Environment variables, which are used in the shell in which they are created and in any child process shells that they derive from. Environment variables can be divided into custom and bash built-in.
Common variables that can only be used in shell functions or shell scripts that create them. Common variables are typically created by developers when developing a script.
4. Environment variables
Environment variables can be set and created on the command line, but the values of these variables are lost when the user exits the command lines.
If you want to permanently save environment variables, you can define them in the. bash_profile or. BASHRC in the user's home directory, or in the global configuration/ETC/BASHRC or/ETC/PROFILE.D.
Some environment variables, such as Home,path,shell,uid,user, have been defined by the/bin/login program before the user logs on.
Typically, environment variables are stored in ~/.bash_profile or/etc/profile.
Three ways to set environment variables:
(1) Export A=value
(2) Declare-x A=value
(3) A=value;export A/;
To set the environment variable to take effect permanently:
(1) User environment variables: ~/.BASHRC and ~/.bash_profile
(2) Global environment variables:/ETC/BASHRC,/etc/profile, and/ETC/PROFILE.D
Tip: (1) When writing Crond timed tasks, it is important to note that the environment variables to be used by the script are best redefined in the executed shell script.
(2) If you want the environment variable to take effect permanently, you can place it in the user environment variable file or the Global environment variable file.
5. Local Variables
Local variables are used in scripts for the user's current shell lifetime. If you start another process or exit in the shell, the value of the variable will not be valid.
Three ways to define local variables:
Variable =value
Variable = ' value '
Variable = "value"
Single-quote ', double-quote ', and non-quoted differences
": What you see is what you gain
"": Parse the value of the variable and then output
No quotation marks: Parse the value of the variable again output, but must be continuous
In general, it is generally recommended to use double quotation marks instead of unquoted.
6, the system to run the shell three ways:
(1) System login Default Shell
(2) non-login interactive shell (e.g. bash, ssh)
(3) Script execution non-interactive shell
Logon Type:/etc/profile=>/etc/profile.d=>/etc/sysconfig/i18n
Non-login: ~/.BASHRC=>/ETC/BASHRC
7. Command definition variables
(1) date
[Email protected] ~]# time= date
[Email protected] ~]# echo $time
May 01, 2018 Tuesday 10:43:51 CST
(2) $ (date)-Recommended use
[[email protected] ~]# time=$ (date)
[Email protected] ~]# echo $time
May 01, 2018 Tuesday 10:44:04 CST

Shell programming in the 3rd Chapter Shell Variable Foundation (PART I)

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.