(Shell note six) shell variable: Shell variable definition, delete variable, read-only variable, variable type

Source: Internet
Author: User

When defining a variable, the variable name does not have a dollar sign ($), such as:

$variableName = "Value"

As shown in figure:

Note 1:

1. There can be no spaces between the variable name and the equals sign, which may be different from any programming language you are familiar with.
2. The name of the variable must be named according to the following rules:
* The first character must be a letter (a-z,a-z).
* The middle cannot have spaces, you can use the underscore (_).
* punctuation cannot be used.
* You cannot use the keywords in bash (you can view reserved keywords with the help command).
6.1. How to use variables

With a defined variable, just precede the variable name with a dollar sign ($), such as:

$your _name= "Andre"
$echo $your _name
andre
$echo ${your_name}

NOTE 2:

The curly braces outside the variable name are optional, plus the curly braces are used to help the interpreter identify the bounds of the variable,
such as the following:
For skill in Ada coffe Action Java 
do
    echo ' I am good at ${skill}script '
done

If you do not add curly braces to the skill variable, write
echo "I am good at $skillScript"
The interpreter treats $skillscript as a variable (whose value is null), and the result of the code execution is not what we expect it to look like.
It is a good programming habit to add curly braces to all variables. 6.2. Redefine variables

A defined variable can be redefined, such as:

#!/bin/bash

va1= "Hello"
echo $va 1
va1= "World"
echo $va 1

It is legal to write, but note that the second assignment cannot be written

$va 1= "World"

The dollar symbol ($) is used when using variables. 6.3. Read-only variable readonly

Use the readonly command to define a variable as a read-only variable, and the value of a read-only variable cannot be changed.
The following example attempts to change a read-only variable, resulting in an error:

#!/bin/bash

va1= "Hello"
echo $va 1
readonly va1
va1= "World"
echo $va 1

Run an error, as shown in figure:
6.4. Delete Variables

Use the unset command to delete a variable.
Grammar:

Unset variable_name

The variable can no longer be used after it has been deleted, unset cannot delete the readonly variable.
As an example:

#!/bin/sh

va1= "Andre"
unset va1
echo $va 1

The above program does not have any result output. 6.5. Variable type

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

Local variables
Local variables are defined in a script or command, only valid in the current shell instance, and other shell-initiated programs cannot access local variables.

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. One part of the shell variable is the environment variable,
Some of them are local variables that guarantee the shell's normal operation.

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.