Shell Tutorial (ii): variable, special variable _shell

Source: Internet
Author: User
Tags numeric value scalar alphanumeric characters


The variable is a string, and we assign a value. The assigned value can be a number, text, file name, device, or any other type of data.



A variable is a pointer that does not exceed the actual data. Shell, you can create, assign, and delete variables. Variable name:



The name of a variable can contain only letters (A through z or A to Z), numbers (0〜9) or underscores (_).



By convention, UNIX shell variables will have their own names in uppercase letters.



The following example is a valid variable name:


_ALI
TOKEN_A
VAR_1
VAR_2

The following is an example of an invalid variable name:


2_VAR
-VARIABLE
VAR1-VAR2
VAR_A!

You cannot use other characters, such as. , * or-these characters have special meanings. To define a variable:



The variables are defined as follows:


Variable_name=variable_value


For example:


Name= "Zara Ali"

The variable name and assigned value defined in the above example "Zara Ali". Variables of this type are called scalar variables. A scalar variable can hold only one value at a time.



The shell can store any value you want in a variable. For example:


VAR1="Zara Ali"
VAR2=100
Access values:


In order to access the value stored in a variable, its name is prefixed by the dollar sign ($):



For example, the following script will access the value defined by the variable name and print it in standard output:


#!/bin/sh
NAME="Zara Ali"
echo $NAME

This produces the following values:


Zara Ali
read-only variables:


The shell provides a read-only command that is marked as a read-only variable. The latter variable is marked as read-only and its value cannot be changed.



For example, the following script will give an error prompt while attempting to change the value of name:


#!/bin/sh
 
NAME="Zara Ali"
readonly NAME
NAME="Qadiri"

This result will produce the following:


/bin/sh:name:this variable is read only.
To cancel the setting of a variable:


Logging off or deleting variables tells the shell to delete the variable list of variables that are tracked. Once you cancel setting the variable, you are not allowed to access the stored value in the variable.



The following is the syntax for defining a variable using the unset command:


Unset variable_name

The above command cancels setting the defined variable value. The following is a simple example:


#!/bin/sh
 
NAME="Zara Ali"
unset NAME
echo $NAME

The example above will not print anything. Variables that cannot be defined using the unset command are not marked as read-only. Variable type:



When a shell runs, there are three main types of variables:



· Local variables: A local variable is a variable that is done in the current instance of the shell. This is not a program started by the shell. Set at the command prompt.



· Environment variable: An environment variable is a shell of any child process that a variable does. Some programs need to be in a normal environment variable. Usually a shell script defines that only the programs required by the environment variables are not running.



· Shell variable: The shell variable is a special variable that is set by the shell and is normally required by the shell. Some synthetic variables are environment variables, while other local variables.









Previous tutorials have said about the use of some non-alphanumeric characters in variable names. This is because the names of the special UNIX variables are used in these characters. These variables are reserved for specific functions.



For example, the $ character represents the process ID number, or PID, in the current shell:


$echo $$


The above command will be written to the PID in the current shell:


29949


The following table shows some special variables that you can use in your shell script:



Variable



Describe



$



The filename of the current script.



$n



These variables correspond to the arguments with which a script is invoked. Here n are a positive decimal number corresponding to the position of a argument (the the ' The ' is $, argument arg Ument is $, and so on).



$#



The number of arguments supplied to a script.



$*



All the arguments are double quoted. If a script receives two arguments, $* is equivalent to $.



$@



All the arguments are individually double quoted. If a script receives two arguments, $@ is equivalent to $.



$?



The exit status is the last command executed.



$$



The process number of the current shell. For shell scripts, this is the process ID under which they are executing.



$!



The process number of the last background command. Command line arguments:



The command-line parameter $, $,... $ is positional parameter, with 0 dollars pointing to the actual commands, programs, shell scripts, functions, and $ $, $,... $ as parameters of the command.



The following script uses various special variables related to the command line:


#!/bin/sh
 
echo "File Name: $0"
echo "First Parameter : $1"
echo "First Parameter : $2"
echo "Quoted Values: $@"
echo "Quoted Values: $*"
echo "Total Number of Parameters : $#"

Here is an example of running the above script:


$./test.sh Zara Ali
File Name : ./test.sh
First Parameter : Zara
Second Parameter : Ali
Quoted Values: Zara Ali
Quoted Values: Zara Ali
Total Number of Parameters : 2
Special Parameters $ * and $ @:


There are special parameters that allow all command-line arguments to be accessed at once. $ * and $ @ will all act the same unless they are enclosed in double quotes "".



These two parameters specify command-line arguments, but the "$ *" Special parameter requires the entire list to be separated as a parameter with a space and "$ @" Special parameter, dividing it into different parameters.



We can write a shell script with an unknown number of $ *$ @ special parameters, as shown in the following command line parameters:


#!/bin/sh
 
for TOKEN in $*
do
   echo $TOKEN
done

There is an example of running the above script:


$./test.sh Zara Ali 10 Years Old
Zara
Ali
10
Years
Old

Note: Here Do...done is a kind of loop that we'll cover in a later tutorial. Exit Status:



$? A variable represents the exit state of the previous command.



Exit status is a numeric value that is returned after completion of each command. As a rule, most commands return if they do not successfully exit the status of 0 if they are successful.



Some commands return other special exit states. For example, some commands distinguish between types of errors and return various exit values depending on a particular type of invalidation.



Successful commands such as the following example:


$./test.sh Zara Ali
File Name : ./test.sh
First Parameter : Zara
Second Parameter : Ali
Quoted Values: Zara Ali
Quoted Values: Zara Ali
Total Number of Parameters : 2
$echo $?
0
$

The shell variable is sufficient to maintain a single value. Variables of this type are called scalar variables.



The shell supports different types of variables, called array variables, that can hold multiple values at the same time. Array, which provides a method for grouping a set of variables. Instead of creating a new name for each variable that you need, you can use a single array to store all the other variable variables.



All naming conventions that discuss shell variables will be named after the array.






from:http://www.yiibai.com/shell/what_is_shell.html#


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.