Application of variables in Linux

Source: Internet
Author: User
Tags builtin
Article Title: Application of variables in Linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

I. Reasons for using variables in Linux

The benefit of a variable is that a simple or easy-to-understand symbol is used to replace another complex or variable data. in short, variables are used for convenience. in Linux, there is too much data in the host to be accessed, and the data is required by some services, and the data is very cumbersome. therefore, to simplify the entire running process, you can use a variable function to change the content based on different users. in this way, the system only needs to obtain the required data based on the variable, without the need to remember the tedious data.

2. query variables in Linux

Query "commands" are external commands (commands provided by other non-bash suites) or built-in commands in bash.

[Root @ localhost ~] # Type-t name

Type indicates whether the name is an external command or a bash built-in command:

File: indicates an external command.

Alias: indicates the name set by the command alias.

Builtin: indicates that this command is a built-in bash command.

Example: type-tpa cd

The system is displayed as builtin, indicating that cd is a built-in command.

3. Application of variables in Linux

(1) Get the variable content

Echo $ variable name

For example, 1. echo $ path indicates the value of the path variable.

2. [root @ localhost ~] # Sum = 30 plus 50-120

[Root @ localhost ~] # Echo $ sum // print "30 + 50-120" instead of-40

30 + 50-120

Echo can also print data to Linux terminals.

For example: [root @ localhost ~] # Echo "haha"

Haha

(2) variable assignment

Method: You can directly use "=" to assign a value to a variable.

For example, if the # echo $ hehe // variable is not assigned a value and the value is directly obtained, the system display is empty.

# Hehe = Vbird

# Echo $ hehe // The system is displayed as Vbird

Attention !!!

1. The equal signs cannot be directly followed by space characters.

2. The variable name can only contain English letters and numbers, but cannot start with a number.

3. If the variable needs to be executed in other subprograms, use export to change the variable to an environment variable, such as export PATH.

4. How to cancel a variable: unset variable name

5. If the variable is not assigned a value and its value is directly taken, the system will be blank.

6. Application of Double quotation marks and single quotation marks in Variables

Double quotation marks can still retain the variable content.

The single quotation mark can only be a common character. $ name will lose the original variable content and only serve as the display type of the character.

# Name = vbird

# Echo $ name // The displayed content is vbird

# Echo "$ name is me" // The displayed content is vbird is me

# Echo '$ name is me' // The display content is $ name is me

(3) Environment Variables

1. List all environment variables and content in the Current Shell environment: # env

HOSTNAME = linux.dmtsai.tw // host name

SHELL =/bin/bash // which program is used in the current environment?

HISTSIZE = 1000 // record the number of commands that have been executed, and RedHat can record 1000

USER = root // current USER name

LS_COLORS // color settings

PATH // execute the file command to search for the PATH. The directories are separated by colons. Note that the file search is based on the PATH value.

PWD // working directory of the current user

LANG // related to the language family

HOME // user's HOME directory (go to the user's HOME directory: cd ~ Or cd)

2. List all variables (including custom variables): # set

In Linux, by default, {uppercase letters} are used to set variables.

PS1 // (Number 1 rather than English letters) Linux Command Prompt settings

$ // The PID (Process ID) of the Shell. query the PID of the Shell and use # echo $

3. Change custom variables to environment variables: # export

(1) # export

Display All environment variables.

(2) variables in Linux can be divided into environment variables and custom variables. the difference between the two lies in that the environment variables are transparent to users, that is, they can be used in any program. the custom variable is only valid in the current Shell. If the current Shell is disabled, the variable is invalid. so in order to make custom variables available in any program, you can use export in the system configuration file/etc/profile,/etc /. declare the variable in bashrc.

Format of the export declaration variable: # Name of the export variable

4. Get the variable from the keyboard: read

To read the variables input from the keyboard, you can use the read command. This command is often used in Shell script writing to communicate with users.

Command Format: # read [-pt] variable name

Parameter description:-p followed by prompt

-T followed by "seconds"

Routine:

1. Ask the user to input the content from the (terminal) keyboard and assign the input content to the variable. after entering the variable, the Linux system will automatically prompt the user to enter it.

[Root @ localhost root] # read yhy // input the content (this is a test !) Assigned the variable yhy.

This is a test!

[Root @ localhost root] # echo $ yhy

This is a test!

2. prompt the user to enter his/her name within 10 seconds and display a friendly interface.

[Root @ localhost root] # read-p "Please input your name:"-t 10 named

At this point, the system prompts:

Please input your name: lss

[Root @ localhost root] # echo $ named // assigns the input content (lss) to the variable named

Lss

5. declare variables using declare

In Linux, declare is used to declare the type of a variable.

Command Format: # declare [-aixr] variable name

Parameter description:-a declares that the variable is an array (array)

-I declares that the variable is an integer)

-X converts the variable to an environment variable, which is similar to export.

-R: Set the variable to read-only. That is, the variable content cannot be changed or unset)

Routine:

1. [root @ localhost root] # sum = 30 + 50 +-120

[Root @ localhost root] # echo $ sum

30 + 50-120

[Root @ localhost root] # declare-I sum = 30 plus 50-120

-40

Note: declare-I is similar to declaring the sum variable as an integer variable in C language.

2. Change sum to an environment variable.

[Root @ localhost root] # delcare-x sum // equivalent to export sum

3. Make the sum variable a read-only attribute and cannot be changed.

[Root @ localhost root] # declare-r sum

[Root @ localhost root] # sum = 30 + 50 + 120

-Bash: sum, readonly variable

6. Restrict File systems and programs

If 10 users log on to a Linux host at the same time, these 10 users open 100 files at the same time, each file size is about 10 MB, the Linux host memory is 10*10*100 = 10 Gb. this will cause the Linux host to crash. to prevent this problem, Linux uses the ulimit command to restrict certain system resources, including the number of opened files, the CPU time available, and the total amount of memory available.

[Root @ localhost root] # ulimit-f 1024 // restrict users to create files of less than 1 MB

[Root @ localhost root] # ulimit-a // list all restricted data

7. set command alias

When the command is very long, you can use an alias to set a new command.

Command Format: # alias command

Routine:

When querying hidden files, you can use # ls-l | more to query the hidden files. However, the input command is cumbersome. Therefore, you can perform the following operations:

# Alias lm = 'LS-l | more'

In this way, an executable command is immediately added. The command name is lm, And it executes ls-l | more.

# Alias vim = vi

In this way, executing the vim editor is equivalent to the vi editor.

8. query executed commands

In Linux, you can use the history command to query commands that you have used before.

[Root @ localhost root] # history // query all commands that have been used

[Root @ localhost root] # history 3 // list the three recently used commands

[Root @ localhost root] # history-w // write history ~ /. Bash_history

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.