[9-13] Shell Series 2--Variable Base

Source: Internet
Author: User

This article is intended to review shell variable definitions, use variables, delete variables, read-only variables, variable types, special variable instances

Knowledge Reserve

    • The variable is essentially a specified area in the memory space, and its data content will change;

    • The variable name essence is a pointer to the memory space of this data change, then in the command execution or operation, only need to pass the variable name, can call that change of data at any time, very convenient

    • The variables in the shell script, like most programming languages, have global and local [for example, a function function]

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/73/7F/wKiom1X_oX6wO-GNAAjaigu8uBE219.bmp "title=" 1111. BMP "alt=" Wkiom1x_ox6wo-gnaajaigu8ube219.bmp "/>

    • It is worth mentioning that the shell is a weakly typed programming language

      • Weak type: variable without prior declaration, directly available [null]

      • Strongly typed: Before using variables, you must specify the variable name and store the format [data type] before you can use


Defining variables and redefining

    • Definition Format: Var_name=value

    • There can be no spaces between the variable name and the equals sign

    • The variable name follows this format ^[_a-za-z] middle section [_a-za-z0-9]*

    • Variable names cannot use punctuation, keywords

    • In a bash environment, you can use set to define variables, but in shell scripts, set commands have special uses!


Example: Define a variable and call

1
2
3
4
5


#!/bin/bash

var1= "I like"
_var2= "computer."
echo  "$var 1 $_var2"

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/73/7D/wKioL1X_pnfyuIsXAAK9GK83738215.bmp "title=" Baidushurufa_2015-9-21_14-31-10.bmp "alt=" Wkiol1x_pnfyuisxaak9gk83738215.bmp "/>

Some people on the internet say that the shell script variable name cannot begin with an underscore, this example illustrates the possible!

Redefining A variable is no different in nature from defining a variable, it only covers it, does not demonstrate

1
2
3
4


#!/bin/bash
var= "1 Yuan qian!"
var="Ten Yuan qian!"
Echo $var


Using variables

    • Direct use ($Var _name), such as:

1
2


Var="I like Fruit"
echo "$var"

    • You can also use ${var_name}, which works the same, but sometimes you can only use this usage!

1
2


Var="I like Fruit"
echo "${var}s."

    • When you want to add a string directly to a variable, you need to use {}, otherwise the variable name error will be resolved. It is recommended to use this usage!


Delete a variable

It's a lot easier to delete variables.

    • Unset var_name


Read-only variables

Some variables once defined do not want to be changed, as read only, can be modified with Read only

1
2
3
4
5
6


#!/bin/bash

mylove="mageedu"
ReadOnly Mylove
mylove="Niaoge"
echo "$mylove"

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/73/7D/wKioL1X_qkzRlPU8AADGpyzJz84452.jpg "title=" Qq20150921144731.png "alt=" Wkiol1x_qkzrlpu8aadgpyzjz84452.jpg "/>


Variable type

When you run the shell, there are three different variables:

Local Variables

A local variable is defined in a script or command and is valid only in the current shell instance.

Environment variables

All programs, including shell-initiated programs, can access environment variables, and some programs require environment variables to keep them running properly. Shell scripts can also define environment variables when necessary.

Shell variables

Shell variables are special variables that are set by the shell program. Some of the shell variables are environment variables, some of which are local variables that guarantee the shell's normal operation.


Special variable instances and delivery

The list of special variables is listed in the [9-13]shell series of basic concepts article, where a demo


1
2
3
4
5
6
7
8


#!/bin/bash

echo  "shell script filename ---> $0"
echo  "current process pid   ---> $$" Span style= "color: #000000;" >
echo  "the number of parameter -> $#"
echo  " First parameter        ---> $1 "
echo 
echo  " last command  state    ---> $? "

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/73/80/wKiom1X_qwXzDEg2AAZCOE0b8c8714.bmp "title=" Baidushurufa_2015-9-21_15-0-6.bmp "alt=" Wkiom1x_qwxzdeg2aazcoe0b8c8714.bmp "/>


This example briefly illustrates the use of special parameters, where 1 2 3 4 5 is passed as the script executes, and I'll talk about

There are 2 command results: Command execution Results and command execution status results

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/73/80/wKiom1X_rXeTU6VdAAhrBCVfbfE197.bmp "title=" Command result. bmp "alt=" Wkiom1x_rxetu6vdaahrbcvfbfe197.bmp "/>

Reference all passed arguments can be in $* and [email protected], or a bit different

When not enclosed by double quotation marks (""), all are "$" and "$" ... All parameters are output in the form "$n".

But when they are enclosed in double quotation marks (""), "$*" takes all the parameters as a whole and outputs all parameters in the form of "$ $ ... $n"; "[email protected]" separates the various parameters to "$" "$" ... All parameters are output in the form "$n".

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/73/7E/wKioL1X_sWSibFVqAAebGHyv4KA566.bmp "title=" Baidushurufa_2015-9-21_15-17-45.bmp "alt=" Wkiol1x_swsibfvqaaebghyv4ka566.bmp "/>

Implementation code

1
2
3
4
5
6
7
8
9
10
11
12
13


#!/bin/bash

Echo"\[email protected] like this--"
forvarinch "[email protected]"
Do
Echo"$var"
Done

Echo"\$* "
forvarinch "$*"
Do
Echo"$var"
Done

This article is from the "Blue Warehouse" blog, be sure to keep this source http://bluebox.blog.51cto.com/8852456/1696771

[9-13] Shell Series 2--Variable Base

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.