Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Overview of shell variable definition learning
Overview of shell variable definition learning
Simply put, it is to save the commands to be executed in text and execute them in order. It is interpreted, which means that no compilation is required.

Rules for defining variable names:

Variable names are strictly case-sensitive, can't have special characters, start with a number, and can't have any spaces on both sides of the equal sign. Learn as much as possible about the meaning of variable names.

1, basic mode

Assign directly to a variable, variable name = variable value.

2. Assign the command execution result to the variable

3. Define variables interactively (read)

Objective: It is more flexible for users to assign values to variables themselves.

Explanation of common options:

-p defines the information to prompt the user.

-n defines the number of characters (limits the length of variable values)

Do not display -s (do not display user input)

-t defines the timeout, and the default unit is seconds (which limits the timeout for users to enter variable values).

4. Define typed variables (declarations)

Objective: to make some restrictions on variables and fix the types of variables, such as integer and read-only.

Usage: declare option variable name = variable value

Common options:

-I declare the variable as an integer. -i A= 123.

-r defines the read-only variable declare -r B=hello.

Unset variable name

Similarity: the contents in quotation marks can be taken as a whole.

Difference: Double quotation marks can refer to variables, while single quotation marks cannot refer to variables.

*: Matches 0 or any number of characters.

? : Any single character

[a-z]: Any single character in parentheses.

! Take the opposite approach

{string, string, string}: Matches all strings separated by commas in brackets.

{1 ... 100}: Match a sequence.

Case:

1. Script name * * *. hush

2. Script content

1) defines the parser.

#! /bin/bash

#! /bin/env bash Advantages: It will automatically find the parser location of the current system.

2) Precautions

#

3) the program itself

Define variables+basic commands+basic grammar+ideas

1. Standard script execution method: relative path or absolute path.

Prerequisite: The standard execution mode script must have executable permissions.

2. Non-standard implementation method (not recommended)

Scenario: View the script execution process; Or the script cannot provide executable permissions.

Parser+script file