Linux System Bash (Shell) Basics (1)

Source: Internet
Author: User

In the previous blog, we introduced some of the features of Shell programming, namely command aliases, command history, shortcut keys, command completion, command line expansion, command execution results, escape characters, wildcards, IO redirection, pipeline operations, etc. This time, we will continue to introduce another major feature of Shell programming--variables;

In shell programs, variables are used to store strings, and shell variables are much simpler than variables in C, with no restrictions on storage classes and types, and no need to be pre-defined before they can be assigned, and "Edge-defined, edge-assigned" when used.

The shell has three types of variables, environment variables, temporary variables and local variables, environment variables are permanent variables, not with the end of the shell script disappears, and the temporary variable is inside the shell program defined, its scope of application is limited to define its program, out of this program can not be used;

program = instruction + data

Data: Those files that are managed in the file system, data stored in memory are called variables

Variable: A contiguous memory space, which is named as a variable name for this memory space, and the device stored in this memory space is called the variable value;

=: assignment operator

Assignment method: Varname=value (without spaces in the middle)

Variable name = string

For example

MyFile in Myfile=/usr/meng is the variable name, = is the assignment number, and the string/usr/meng is the value assigned to the variable. There are no spaces on both sides of the equals sign;

Naming conventions for VarName:

1. Can only start with the letter or underscore "_", cannot use the number, can be any alphanumeric and underline;

2. Strict case sensitivity;

3. See the meaning of the name;

4. Writing Specifications

1) All caps: FILES

2) Hump Type: MyFile (the beginning of each word is capitalized)

5. Cannot be the same as the known variable name (otherwise equivalent to replacing the existing variable name)

Depending on the programming language, variables can be divided into two categories:

Strong variables:

Must be declared before use, and declare the type of the variable, strictly distinguish the type of the variable;

Weak variables: can be used at any time to declare, or even do not declare the use of the variable type does not require, if not specified variable type, uniform according to the character type treatment;

The variables of bash are weak variables;

C C++java and so on are strong variables;


The value of the reference variable: $ (VARNAME)--$VARNAME (some special scenes must write curly braces) also known as variable substitution;

When using the value of a variable in a program, to precede the variable name with a $ character, he tells the Shell to replace the variable value;

Cases

#dir =/usr/meng

#echo $dir

#/usr/meng (Show results)

Command substitution:

You can assign the result of a command to a variable in two ways.

One form is:

#varname= ' command table '

Cases

$dir = ' pwd '

Another form:

#varname=$ (command table)

Cases

$dir =$ (PWD)


Types of Bash variables:

Global variables (Environment variables): mainly refers to the scope of action, the scope of the entire shell process, including its child shell;

Local variables: The scope of action is just the landing shell, not including the child shell;

Local variables: The scope is only the current program segment is generally used for functions;

Default Global variables:

Positional parameter variable: Indicates the location of the program called by the program; $1,$2,$3 ... $n

Special variables:

$?,$$,$#,[email protected],$* ...

$?: Expands the status return value (Exit status) for the last command executed

$#: Number of positional parameters (in decimal notation);

$*: All positional parameters are written consecutively starting from 1, when expanded with double quotation marks, it expands to a value separated by the first character of the special variable "IFS", and the content displayed is a number of different values;

[Email protected]: Starting from 1 to write all positional parameters, when using double quotation marks to expand, it expands into a separate string;

$$: Expands the process identifier for the current shell;

$: Expands the file name for the shell or shell script;

Cases

#echo

-bash

Use of variables:

variable assignment: varname= ' value ' (belongs to local variable)

Undo Variable: #unset VARNAME

declaring environment variables:

Export VARNAME (exported, can be applied to all child shells)

Export Varname=value

Example

[[email protected] ~]$ exportb= ' Hello '

[Email protected] ~]$ echo $b

Hello

[Email protected] ~]$ bash

[Email protected] ~]$ echo $b

Hello

[Email protected] ~]$

declare:

-A: Declaring an indexed array (if supported)

-A: Declare associative arrays (if supported)

-I : declare shaping variables;

-L: Declares the variable and converts the value of the variable to lowercase letters;

-U: Declares a variable and converts the value of the variable to uppercase;

-X: Declare the variable and export it as an environment variable (global variable);

-R: Sets read-only variables and cannot be undone using unset, only bash is turned off;

Cases

[Email protected] ~]$ declare-l s= ' HELLO '

[Email protected] ~]$ echo $s

Hello

[Email protected] ~]$ declare-u s= ' Hello '

[Email protected] ~]$ echo $s

HELLO

To view environment variables:

Set

Export

Env:

To declare a local variable:

Local Varname=value (for functions, only valid in the code snippet of the function, which fails when the function call is complete)

Configuration file:

Common configuration file:/etc/bashrc,/etc/profile

Private profile: ~/.bashrc,~/.bash_profile

Putting the variable into the configuration file is permanent;

Note: The general definition of variables are first-use, do not need to change the configuration file, if you modify the declaration of variables, it is possible to cause system vulnerabilities, not worth the candle, so do not do not change;





Linux System Bash (Shell) Basics (1)

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.