Linux: Part 6: Shell

Source: Internet
Author: User

1. Variable type in Shell

Shell variables can be divided into environment variables, location variables, pre-defined special variables, User-Defined variables,

Environment variables in shell are shell predefined variables used to set the system runtime environment. environment variables are named by the system. The value of some system variables is set by the system, and the value of some environment variables can be specified by the user.

The environment variable name consists of uppercase letters. Common shell environment variables are as follows:

Home: the full path name of the user's home directory. CD $ home can be switched to the user's home directory.

Path: similar to the path in windows, shell searches for binary executable files in sequence.

Echo $ path can display the current path. Add the new path method $ Path = $ path: New Path

Term: terminal type echo $ term

PS1: prompt. The default value is # for root users and $ for common users.

Shell: absolute path of the shell interpreter

LOGNAME: username of the login user

UID: uid echo $ uid of the current user

Location variables are determined based on the location of the parameters that appear on the command line. In the command line that calls the shell program, the parameter location is defined as follows.

$ Command parameter 1 parameter 2 parameter 3

Where $0 corresponds to the name of the executed command

$1 corresponds to parameter 1

$2 corresponds to parameter 2

$3 corresponds to parameter 3

Predefined special variables have special meanings and cannot be changed. All predefined variables are composed of the "$" symbol and another symbol. The common predefined special variables are as follows:

$ #: Number of location parameters (excluding shell script names)

$ *: A string composed of location parameters

$! : Process number corresponding to the previous background command

$? : The exit status of the previous command, which is a decimal number. If the return value is 0, the execution is successful.

$: Current process ID PID

Requirement: The variable name must start with a letter or underline followed by any number of letters, numbers, and underscores.

Valid: _ name size my_idea

Two built-in commands declare and typeset can be used to create variables. You can also set the variable creator by setting the options of the command.

Variable assignment and reference

In shell programming, variables do not need to be declared in advance, and the naming of variable names must follow the following rules:

# The first character must be a letter (a-Z, A-Z)
# No space in the middle. You can use underscores (_)
# Punctuation cannot be used
# You cannot use keywords in bash (you can use the help command to view reserved keywords)

When you need to assign a value to a variable, you can write it like this:

Variable name = Value

To use the value of a variable, you only need to add a $
(Note: when assigning values to variables, you cannot leave spaces on both sides of "=)

The system provides the unset command to delete variables, such as unset name.

There are five values assigned to variables: read command, direct value assignment, command line parameters, command line output results, read from the file.

Use the READ command read name

Direct assignment
A = "Hello World" # No space exists on both sides of the equal sign

Echo "A is:" $

Assign a value using the command line parameter (This assignment method is used when parameters need to change frequently and do not need to interact.) Parameters after execution

This method is suitable for processing large volumes of data, writing the corresponding data directly to the file, and then running the script.

Generally, a row of data is read through a while loop, that is, a row of data is read from the file until the end of the file is read.

For example:

#! /Bin/bash

Ls *. Sh> execfile

While read line

Do

Chmod A + x $ line

Done <execfile

This script uses the while... do... done structure, and the last line indicates reading data from the file execfile. While read line indicates that a row of data is read cyclically and assigned to the variable line.

 

Three quotation marks are used when a string is contained: single quotation marks, double quotation marks, and reverse quotation marks.

I. Overall reference

1. mv "~ File name.txt "another.txt

In this case, there is no difference between single double quotes and they have the same meaning.

2. replace variables

1. Echo "the value of '$ {var}' is $ {var }".

In this case, when a variable is used in a sentence enclosed by double quotation marks, it is replaced with its content. When using single quotation marks, variable replacement does not occur. Therefore, single quotes are strongly referenced. The output result of the example above is:

1. The value of '$ {var}' is BBB.

Iii. Command replacement

1. Var = 'uname-R'

In this way, the value of the variable $ VaR is the output of the command uname-R.

Matching is more advanced than replacing variables. For example, $ {F % JPG} uses JPG to match the variable F from the end, and matches the matching part (that is, the extension) when two percentage signs are used, the maximum matching method is used, for example, "abcdabcd". If a * D is used for matching, it will match all 8 characters, and the minimum match will match 4 characters. In contrast to the percent sign, you can also use "#" to match variables from the front to the back, or use two to represent the maximum match.

Let's look at an example.

1 .#! /Bin/bash

2. for VAR in *. Sh

3. Do

4. Echo "$ {var % sh }"

5. Done

The result is as follows:

1. test1.

2. test2.

3. test3.

4. Test.

The suffix is missing and can be used to modify the suffix name.

1 .#! /Bin/bash

2. for VAR in *. Sh

3. Do

4. mv "$ {var}" "$ {var % sh} sh"

5. Done

In this way, the lowercase SH is changed to uppercase.

 

Echo

Echo hello #
No variable reference, just a hello string.
Echo $ hello
Echo $ {Hello} // same as above.
Echo "$ hello"
Echo "$ {Hello }"
Hello = "a B C D"
Echo $ hello # a B c d
Echo "$ hello" # a B c d

Echo '$ hello' # $ hello

 

 let
  let a=16+5
 
References: multiple locations. Please add them.

 

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.