Linux Shell series (3) Shell variable, linuxshell

Source: Internet
Author: User

Linux Shell series (3) Shell variable, linuxshell

This article is part 3 of the Linux Shell series. For more shell tutorials, see the Linux Shell series tutorials.

As an advanced script language, Shell also supports custom variables. Today, we will introduce the variable-related knowledge in Shell.

To make Shell programming more effective, the system provides some Shell variables. Shell variables can save variable names such as pathnames, file names, or numbers.

Shell regards any settings as text strings. There are two types of variables: local and environment. Strictly speaking, there can be four types, but the other two types are read-only and can be considered as special variables, which are used to pass parameters to Shell scripts.

Define Variables

When defining a variable, the variable name does not contain the dollar sign ($), for example:

VariableName = "value"

Note that there is no space between the variable name and equal sign, which may be different from all programming languages you are familiar. Variable names must follow the following rules:

  • The first character must be a letter (a-z, A-Z ).
  • There cannot be spaces in the middle, and you can use underscores (_).
  • Punctuation cannot be used.
  • You cannot use keywords in bash (you can use the help command to view reserved keywords ).

Example of variable definition:

MyUrl = http://www.linuxdaxue.com

MyNum = 100

Use Variables

To use a defined variable, you only need to add the dollar sign ($) before the variable name, such:

your_name="linuxdaxue"echo $your_nameecho ${your_name}

The curly braces outside the variable name are optional and can be added without adding them. The curly braces are used to help the interpreter identify the boundary of the variable, for example:

for skill in Ada Coffe Action Java do    echo "I am good at ${skill}Script"done
 

If you do not add curly brackets to the skill variable and write it as echo "I am good at $ skillScript", the interpreter regards $ skillScript as a variable (its value is null ), the code execution result is not what we expect.
We recommend that you add curly braces to all variables. This is a good programming habit.

Redefinition of Variables

The Defined variables can be redefined, for example:

myUrl="http://www.linuxdaxue.com"echo ${myUrl}myUrl="http://www.linuxdaxue.com"echo ${myUrl}
 

This write is legal, but note that the second value cannot be written $ myUrl = "http://www.linuxdaxue.com", only the dollar sign ($) when using the variable ).

Read-Only variables

The readonly command can be used to define a variable as a read-only variable. The value of a read-only variable cannot be changed.
In the following example, an error is returned when you try to change the read-only variable:

#!/bin/bashmyUrl="http://www.linuxdaxue.com"readonly myUrlmyUrl="http://www.linuxdaxue.com"
 

Run the script. The result is as follows:

/bin/sh: NAME: This variable is read only.
Delete variable

You can use the unset command to delete variables. Syntax:

Unset variable_name

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

#!/bin/shmyUrl="http://www.linuxdaxue.com"unset myUrlecho $myUrl
 

The above script has no output.

Show all local shell Variables

Use the set command to display all locally defined Shell variables.

Variable type

When running shell, there are three variables at the same time:

1) local variables

Local variables are defined in scripts or commands and only valid in the current shell instance. Other programs started by shell cannot access local variables.

2) Environment Variables

All programs, including those started by shell, can access environment variables. Some programs require environment variables to ensure normal operation. When necessary, shell scripts can also define environment variables.

3) shell special Variables

Shell variables are special variables set by shell programs. Some shell variables are environment variables and some are local variables, which ensure the normal operation of shell. The following describes the Special variables in shell.

Shell special Variables

The special variables in Shell mainly include the following:

$0, $ #, $ *, $ @, $ ?, $

The following describes the variables, meanings, and usage methods respectively.

Name Description
$0 File Name of the current script
$ # The number of parameters passed to the script or function.
$ * All parameters passed to the script or function.
$ @ All parameters passed to the script or function. The double quotation marks ("") are slightly different from $.
$? The exit status of the previous command or the return value of the function.
$ The ID of the current process. For Shell scripts, the process ID of these scripts
$ N Parameters passed to the script or function. N is a number that represents the number of parameters. For example, the first parameter is $1, and the second parameter is $2.
Command Line Parameters

The parameter passed to the script when running the script is called the command line parameter. The command line parameters are represented by $ n. For example, $1 indicates the first parameter, $2 indicates the second parameter, and so on.

$0 indicates the file name of the current script.

The following example shows the differences between these parameters:

#! /Bin/bashecho "File Name: $0" echo "first parameter: $1" echo "second parameter: $2" echo "All parameters: $ @ "echo" All parameters: $ * "echo" parameter count: $ #"
Run the command./test. sh LinuxDaxue. com.

Execution result:

File Name:./test. sh first parameter: LinuxDaxue second parameter:. com all parameters: LinuxDaxue.com parameter count: 2
$ * And $ @

$ * And $ @ indicate all parameters passed to the function or script. If they are not included in double quotes (""), they are all included in "$1" "$2 "... All parameters are output in the form of "$ n.

However, when they are enclosed by double quotes (""), "$ *" takes all parameters as a whole, starting with "$1 $2... $ N is used to output all parameters. "$ @" separates parameters and uses "$1" "$2 "... All parameters are output in the form of "$ n.

$? Get exit status

$? You can get the exit status of the previous command.

The exit status is the result returned after the previous command is executed.

The exit status is a number. Generally, if most commands are successfully executed, 0 is returned. If the command fails, 1 is returned.

However, some commands return other values, indicating different types of errors.

 

For more shell tutorials, see the Linux Shell series tutorials.

Fixed Link: linux Emy network-Linux Shell series tutorial (3) Shell variable

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.