Shell Learning-basics (variables, inputs, outputs, arrays, environment variables)

Source: Internet
Author: User
Tags arrays

1, #. /bin/sh Opening

2. Variables:

(1) Definition: variable name = string. Note: = no spaces around; name= "Hello World" for which a string with spaces is assigned;

(2) Citation: Use $nametest, ${name}test.

3. Wildcard characters:

(1) * symbol: Used to match the string 0 times or more occurrences; the matching. and \ symbols must be indicated. such as *test cannot match. EXtest, but to use. Ex*;*test can not match/home/test, to use/*/test.

(2). Symbol: matches only one character in the corresponding position

(3) [] symbol: matches any one of the characters in the range specified by the character group

(4). Symbol: matches any one character that is not within the bounds of the character group

4. Shell input:

Read Variable 1 variable 2

#!/bin/sh
echo "Input your name and age:"
Read Name Age
echo "Name is:" $name
echo "Age is:" $age

5. Shell output:

Echo

6. Shell array: Supports one-dimensional array, but does not qualify the size of the array.

(1) General way to take value: ${array name [subscript value]}

(2) Assignment method: array name [subscript value]= Value

(3) can be assigned a value, but also the overall assignment, but the value and value are separated by a space. Array name = (value1 value2 value3 ... )。

eg

#!/bin/sh
Name= (I am very happy)
echo "Name[0] is:" ${name[0]}
echo "name[1] is:" ${name[1]}
echo "name[2] is:" ${name[2]}
echo "Name[3] is:" ${name[3]}

(4) You can output the value by subscript, or you can use the * and the @ symbol instead of subscript, such as ${name[*]} to take out the values of all the elements in the array.

#!/bin/sh
Name= (I am very happy)
echo "Name is" ${name[*]}
echo "Name1 is" ${name[@]}

(5) above is how to add, read, the following describes how to modify, delete.

A. Modify the re-assignment of the specified elements to an array;

B, delete need to use an external command: unset. unset name[0] To empty the element labeled 0, or you can use the unset name[@] command to cancel all elements in the array.

#!/bin/sh
Name= (I am very happy)
Name[0]=myself
Echo ${name[@]}

Unset Name[0]
Echo ${name[@]}

Unset name[@]
Echo ${name[*]}

Name[0]=hello
Name[1]=world
Echo ${name[*]}

Operation Result:

Myself am very happy
Am very happy

Hello World

6. Environment variables: There is a public space in Linux that is dedicated to storing environment variables that users can view through printenv.

(1) Basic Environment variables:

PWD, HOME, LOG NAME, SHELL, PATH

(2) Deletion of environment variables:

With the unset command, Eg:unset path is empty.

#!/bin/sh
echo "PWD:" $PWD
echo "Path:" $PATH
echo "LogName:" $LOGNAME
echo "SEHLL:" $SHELL
echo "Home:" $HOME

Operation Result:

Pwd:/home/test/shell
Path:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
Logname:root
Sehll:/bin/bash
Home:/root


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.