Getting started with Shell: Shell variables, getting started with shell Variables

Source: Internet
Author: User

Getting started with Shell: Shell variables, getting started with shell Variables
Shell entry (1): the first program of Shell variable Shell script

Log on to the console using the Linux User Name and enter the ls command:

Create a test directory in the current file directory and a hello. sh file in the test directory.

Create the test directory
mkdir test
Go to the test directory and create the hello. sh file.
Cd testtouch hello. sh or use vimvim hello. sh # To directly create the hello. sh file and open
Use vim to open hello. sh and write shell commands
#!/bin/bashecho "Hello World!"

Execute shell commands

During execution, the file has no permission. Therefore, view the File Permission before execution.

ls -l

No permission. Modify the permission.

chmod 700 hello.sh

Execute file hello. sh

./hello.sh

Shell variable

Shell variable naming rules:

Only English letters, numbers, and underscores can be used for naming. The first letter cannot be a number. You cannot use the keyword name in bash.

When defining a variable, the variable name and equal sign cannot have spaces.

Use of Shell Variables

To use a variable, you only need to add the dollar sign $ before the variable name. For example:

your_name='tom'echo $your_name

Output: tom

Or use the form of $ {var}, such:

echo ${your_name}

Output: tom

Read-Only variables

You can use the readonly command to define a variable as a read-only variable. The value of the read-only variable cannot be modified.

id='007'readonly idid='008'

Output result: bash: id: readonly variable

Delete variable

You can use the unset command to delete variables.

unset varibale_name

After a variable is deleted, it cannot be used again. The unset command cannot delete Read-Only variables.

Variable type

Local variables: defined in scripts or commands. They are valid only in the current shell example. programs started by other shells cannot access local variables.

Environment variables: all programs, including shell Start Programs, can access environment variables. Some programs require environment variables to ensure normal operation.

Shell variable: The shell variable is a special variable set by the shell program. Some shell variables are environment variables and some are local variables, which ensure the normal operation of shell.

Shell string

A string is the most common data type in shell. A string can be enclosed in single quotation marks, double quotation marks, or no quotation marks.

Single quotes

str='hello'

Any character in single quotes is output as is, and the variable in single quotes is invalid.

Single quotes cannot appear in single quotes (not after escape)

Double quotation marks

Advantages of double quotation marks:

Variables can exist in double quotation marks.

Escape characters can appear in double quotation marks

Concatenated string

[zhang@localhost ~]$ date='today is 2018-01-16'[zhang@localhost ~]$ name='tom'[zhang@localhost ~]$ str="hello,$name "[zhang@localhost ~]$ echo $str$datehello,tom today is 2018-01-16

Returns the string length.

zhang@localhost ~]$ string='abcd'[zhang@localhost ~]$ echo ${#string}4

Extract substring

[zhang@localhost ~]$ string='hello world'[zhang@localhost ~]$ echo ${string:1:4}ello

Search for substrings

[zhang@localhost ~]$ string='hello world'[zhang@localhost ~]$ echo `expr index "$string" o`5[zhang@localhost ~]$ echo `expr index "$string" w`7

Shell Array

Bash supports one-dimensional arrays (multidimensional arrays are not supported), and the array size is not limited.

Define an array

In Shell, brackets are used to represent arrays. array elements are separated by space characters. The general format of the defined array is:

Array name = (value1 value2 value3... valuen)

For example:

arr=(value0 value1 value2 value3)

Or

arr=(value0value1value2value3)

You can also separately define the components of an array.

arr[0]=value0arr[1]=value1arr[2]=value2arr[3]=value3

Read Array

The general format for reading array element values:

$ Array name [subscript]

For example:

value0=${arr[0]}

Use the @ symbol to obtain all elements in the array. For example:

echo ${arr[@]}

Returns the length of an array.

Length =$ {# arr [@]} or length =$ {# arr [*]}

Obtains the length of a single element in an array.

length0=${#arr[0]}

Shell comment

The line starting with "#" is a comment and will be ignored by the interpreter.

Sh contains no redundant rows. Only one "#" can be added to each row, for example:

#! /Bin/bash # -- encoding UTF-8 -- # comment

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.