Learn the shell script

Source: Internet
Author: User

First line: #!/bin/bash--#! Interpreter for Tag script

Script: #!/bin/bash

echo "Hello World"

Give execute permission chmod +x./test.sh

Execute script:./test.sh

The $ symbol is not required when defining a variable: td= ' Date ' +%y%m%d '

1. There can be no spaces between the variable name and the symbol
2. The first character must be a letter
3. The middle cannot have the space, can use the dash (_)
4. Punctuation and keywords in bash are not available

In addition to displaying assignments, you can also use statements to assign values directly to variables

For file in ' ls/etc ' to loop the file name in/etc directory

With a defined variable, just precede the variable name with the $ symbol to echo $TD or Echo ${TD}

The same variable name can be assigned two times eg:td= ' date ' +%m%d "'

Read-only variable: readonly defines variable as read-only variable, variable value cannot be changed

Url= "Http://www.baidu.com"

ReadOnly URL

Url= "http://www.tx.com"--Error

Delete variable: unset variable_nae, cannot delete read-only variable

Shell has three variables: ① local variable: defined in script or command, only valid in current script
② environment variables: All programs are valid
③shell variables: Variables set by shell programs, with local and environment

A single quote string cannot have a variable, cannot do character escaping, and double quotation marks can be implemented.

Your_name= "World"

Stitching string greeting= "Hello,${your_name}!"

get string length ${#greeting}

extract 1-4 of the string, subscript starting from 0 ${#greeting: 1:4}

Find string $ (expr index "${greeting}" world)

Reverse quotation marks for command substitution: td= ' date ' +%y%m%d ' ' Equals $ (date "+%y%m%d")

./test.sh 1 2 3

File name executed: +---/test.sh

The first parameter is:--1

The second parameter is: $2

The third parameter is: $3-+

The number of parameters is: $#-3

The current process that the script runs id:$$-->8010

Show all parameters passed: $*-->1 2 3

$* and [email protected] Difference: $* equivalent to "1 2 3", equivalent to pass a parameter, [email protected] equivalent to "1" "2" "3" equivalent to pass three parameters

Variable_name

Learn the shell script

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.