Linux location parameter variable, linux Parameter Variable

Source: Internet
Author: User

Linux location parameter variable, linux Parameter Variable



Understanding of various variables in linux

Technically, variables can be divided into two types: environment variables and shell variables.

1. Environment variables can be regarded as global variables, independent from the specific shell. They are fixed variables in the system (values may not be the same ). The env command can view all environment variables. The export command can modify existing environment variables or export a shell variable as an environment variable. However, the effect of export is limited to this login operation, that is to say, the next login will become invalid.
In addition, environment variables and internal variables should be the same thing.

2. Shell variables can be regarded as local variables and only valid for the current shell. Child processes do not inherit them.

User variables. I understand them as User-Defined variables. Of course, they are shell variables. For example, enter the following command line:
$ A = 123
$ Echo $
123
The first line generates a user variable a with a value of 123, the second line shows the value of variable a, and the third line shows the execution result.

Local variables. I understand all the variables of the Current shell, including environment variables and shell variables (including the variables and user variables provided by the shell itself ). The set command can view all local variables.

The parameter variable should be the location variable, which is easy to understand and can be understood in an example. Create a script test with the following content:
#! /Bin/bash
Echo "$1"
Then execute this script (the test permission must be modified to be executable ):
$./Test helloworld
Helloworld
The helloworld In the first line provides the value of $1 in the script, and the helloworld in the second line is the execution result.

When passing a parameter to a script, you can use this location variable to obtain the parameter.
They are:
$0: Script Name. This variable contains the address. You can use basename $0 to obtain the Script Name.
$1: The first parameter.
$2, $3, $4, $5,... and so on.

What are the types of shell variables and their usefulness in linux?

2.1.1 local variables
Definition: Used in your current shell lifecycle script;
Example: [redhat @ server redhat] $ name = "Red Hat Linux" # Set the variable to Red Hat Linux
[Redhat @ server redhat] $ echo $ {name} or # DISPLAY variable content
= [Redhat @ server redhat] $ echo $ name #
Red Hat Linux # display the settings,
[Redhat @ server redhat] $ set | grep "name" # view the content just set, or view all the local variables of the user without 'grep "name,
[Redhat @ server redhat] $ readonly name # Set the variable name as a read-only variable and cannot be set again. For more information, see variable definition ;-_-!
[Redhat @ server redhat] $ name = "He SenLin" # assign a value again
Bash: name: readonly variable # Read-Only File System
[Redhat @ server redhat] $ readonly # view read-only files in the system
Appendix: log on to the system again and use
[Redhat @ server redhat] $ echo $ {name}
# The local variable does not exist,
It indicates that the local variables can only survive the user's shell lifecycle, and the logon attempt is no longer available,
2.1.2 Environment Variables
Definition: environment variables are used for all user processes, including editors, scripts, and applications. The logon process is the parent process, and all user processes executed in shell are child processes, unlike local variables, they are only used in the current shell.
The existing Directory is in the home directory of all users <that is,/home>/. bash_profile or
How to Set environment variables? The first method is to edit the above two files and use "export" to set or change them. This is not as simple as local variables, but just name = "Red Hat Linux, however, you can change the local variable to the environment variable. You only need to enter: export <variable name> after the local variable ends. For example, we can change the name from the local variable to the environment variable, we can do this;
[Root @ server root] # name = "Red Hat Linux" # local variable
[Root @ server root] # export name # The variable name is upgraded from local to Environment
Of course, you can also use the following method;
[Root @ server root] # export name = "Red Hat Linux" # define name as an environment variable;
After definition, you can view it through env. I don't know which command is used to view the local environment variables above. Do you remember this ?, This example shows how to view the currently defined environment variables;
[Root @ server root] # env | grep "name"
Red Hat Linux
Similarly, if grep "name" is not added, this parameter is used to view all environment variables. Similarly, we can run readonly to give the name a read-only attribute;
Tips: environment variables are all contained in local variables <because it can be run in the parent process or in the child process>. Note that local variables cannot run in all processes, only environment variables are allowed. If you don't understand it, think of people and nature ;-_-! Or not? TIPS:
[Root @ server... the remaining full text>

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.