Shell--1.shell Correlation and variables

Source: Internet
Author: User
Tags array length

1.shell Script Interpreter

Bourme Shell (/usr/bin/sh or/bin//bin//usr/bin//usr/bin/ for Root (/sbin /SH)

Bash is widely used in the development environment because it is easy to use and free

2. First shell script

#!/bin/"Hello World"

#! is a contract tag that tells the system what interpreter the script needs to execute.

3. There are 2 ways to run the script,
(1) One is to save the above content to test.sh

chmod +x test.sh. /test.sh


(2) The other is to run the interpreter directly, whose parameter is the file name saved above

/bin/sh test.sh or /bin/bash test.sh

This way does not need to write the Interpreter information (#!/bin/bash) in the script, it is useless to write

4. Shell variables

(1) Define the rules:
(1) can contain letters, numbers, underscores, cannot have punctuation
(2) must start with a letter or an underscore
(3) Case sensitive
(4) cannot contain keywords inside bash

(2) using variables
Using a defined variable, simply add a $ symbol to the front
Such as:

Myname= "LVYF""$myname""${myname}"

Curly braces, plus curly braces, allow the interpreter to recognize the bounds of a variable, such as:

echo "This was MyName $mynameHaha""This is MyName ${myname}haha"

(3) Re-assign value

Myname= "LVYF""${myname}"myname= "Lvyafei""${myname}"

The second assignment does not add $, only the $ symbol when the variable is used

(4) Read-only variables

Myname= "LVYF""${myname}"readonly myname myname= "Lvyafei"

Execution will error:./first_shell.sh: Line 8:myname: read-only variable

(5) Deleting variables

unset Mynameecho ${myname}

It won't output anything.

(6) Variable type
1) Local variables: defined in script or command, valid only in the current shell instance, other shells cannot access the local variable
2) Environment variables: All programs, including Shell Launcher to access environment variables, some programs require environment variables to ensure that they run correctly, such as in the script to start Nginx, the environment variables must have nginx related configuration
3) Shell variable: is a special variable set by the shell program. Part of the environment variable is a local variable, which guarantees the shell's normal operation.

5. Shell string, string may be in single quotes, double quotes, or without quotation marks

(1) Single quotation mark
1) All characters in single quotes are output as-is
2) The variable of single quotation marks is invalid as

str1= ' abc 'str2= ' Hello ${str1} ' This paragraph will output Hello ${str1}


3) Single quotation mark cannot be enclosed in single quotation mark, escape character single quotation mark (\ ')

(2) Double quotation marks
1) Double quotes can have variables
2) Double quotes can appear escape characters

Str= "LVYF""Hello \" ${str}\ "Heheh""LVYF" Heheh

(3) Anti-quote ' 1 left button

The anti-quotation marks contain commands that are typically ordered to the shell that is currently executing, such as:

Str= "LVYF""Hello \" ${str}\ "Heheh ' Date '" LVYF "Heheh July 25, 2016 Monday 18:22:47 CST


(4) Stitching strings

Your_name= "Lvyafei"greeting= "Hello ${your_name}"echo ${greeting}

(5) Get string length

echo ${#greeting} #输出13

(6) Intercept string
Starting with the 1th character, intercept 5 characters:

Echo ${greeting:0:5} #输出hello


6. Shell Array

1) Defining an array
In the shell, the array is represented by parentheses, and the array elements are separated by spaces, defining the general form of the array

Arrt_name=(val1 val2 val3) or arry_name=(VAL1VAR2VAL3VAL4) or arry_name[0]=val1arry_name[ 3]=Val3 can not use successive subscripts, and there is no limit to the subscript range


2) reading an array

${arry_name[0]}

Use @ To read all elements of an array

arry[1]=10    arry[3]=2020


3) get the array length

${#arry_name [@]}${#arry_name[*]}${#arry_name [n]}


7.shell Notes # # #

8.shell parameter passing

1) $# The number of arguments passed to the script
2) $* all parameters passed to the script
3) [email protected] All parameters passed to the script
4) $? Displays the exit status of the last command, 0 means no error, and any other number indicates a problem

5) ID number of the current process $$ script is running
6) $! ID number of the last process running in the background
7) $-Displays the current options used by the shell, which are identical to the SET command function.

the difference between $* and [email protected] If 3 parameters are passed A,B,C, use $* thought to pass a parameter ABC and use [email protected] will think 3 parameters A, B, c



Shell--1.shell Correlation and variables

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.