Linux--shell programming variable assignment and reference __arduino

Source: Internet
Author: User
Linux--shell Programming variable assignment and reference

Linux shell programming is a very mature programming language that supports various types of variables. There are three main types of variables: environment variables, internal variables, and user variables. Environment variables (environment variable) are part of the system environment and do not have to be defined. You can use them in a shell program, and some variables (such as path) can also be modified in the shell. Internal variables (built-in variable) are provided by the system. Unlike environment variables, you can't modify them. The user variable (username variable) is defined when you write a shell script. You can use them and modify them in a shell program.

The main difference between shell programming and other programming languages is that in shell programming, variables are of a non type nature. That is, you do not have to specify whether a variable is a number or a string. User Variables

In shell programming, user variables need not be declared in advance, and variable names must be named in accordance with the following rules:

1. The first character must be a letter (a-z,a-z);
2. There can be no space in the middle, use the underscore (_);
3. Punctuation marks cannot be used;
4. You cannot use keywords in bash (you can view reserved keywords using the help command). variable Assignment

Variable Assignment format:

Variable name = value
Accessing variable values

To take the value of a variable, simply precede the variable name with a $.

(Attention:don ' t keep blank between the variable with the equal ' = ') examples

# Assigning values to variables:
A= "Hello World" #等号两边均不能有空格存在

# Print the value of variable a:
Echo-e "A is: $a \ n"


1. Bash in the variable assignment, the equal sign on either side can not have spaces exist;

Use your favorite editor, enter the above and save it as a file Test_hello.bsh, and then execute chmod +x test_hello.bsh to have execution permissions, and then enter./test_hello or bash Test_ HELLO.BSH executes the script.

Program Run Result:

A Is:hello World

Sometimes variable names can be confused with other text, such as:

echo "This is the $numst"

The script above does not output "This is the 1st";

This is because the shell is going to search for the value of the variable numnd, but the variable is not actually assigned a value.

We can use curly braces to tell the shell to separate the NUM variable from the other parts:

echo "This is the ${num}st"

Program Run Result:

This is the 1st

Position Parameters


can write a shell script that accepts several parameters when it is invoked from the command line or from another shell script. These options are provided to the shell program via Linux as positional parameters (position parameter). The location parameter has a private name given by the system. The first parameter, stored in a variable, is named 1 (the number 1), which can be accessed in a program, with the second parameter named 2 in a variable, in a program that can be accessed by $, and so on. When calling a shell program, you can omit one or more numeric positional arguments.

For example, if the shell program MYPGM1 requires two parameters (such as first and last name), you can invoke the shell program with just one argument (name).    However, you cannot invoke a shell program with only the second argument (last name). #!/bin/bash #name Display program if [$#-eq 0];   Then echo "Name not provided."   else echo "Your name is $." Fi
In bash, you can perform mypgm1 as follows:
# . Mypgm1
Then you get the following output:
Name not provided.
However, if you perform mypgm1 as follows:
# . MYPGM1 David will then get the following output:
Your name is David.
Shell Program MYPGM1 also illustrates another aspect of shell programming, that is, internal variables. In MYPGM1, the variable $ #是一个内部变量, which provides the number of positional arguments passed to this shell program.
Internal variables
Internal variables are a special type of variable provided by Linux, which is used in the procedure to make judgments. The value of such variables in a shell program cannot be modified.

Some of the internal variables are summarized as follows:

Table 1.1. Common internal variables

$ Equivalent to the C language main function argv[0]
$, $ ... These are called positional parameters (positional Parameter), which correspond to the argv[1 of the C language main function, argv[2] ...
$# Equivalent to the argc-1 of the C language main function, note that the # is not followed by a comment
$@ Indicates that the argument list "$" ..., for example, can be used in the back of the For loop.
$? Exit Status for previous command
$$ The process number of the current shell

To illustrate the use of these variables, here is a demonstration program called MYPGM2: #!/bin/bash #my test Programs echo "number of parameter is $#" echo "" " $ "echo" Parameters as a single string is $* "

In bash, if you execute mypgm2 from the command line, as follows: #. MYPGM2 David Victor

You get the following result: number of parameters is 2   program name is  mypgm2   parameters as a single string is david victor  Armed with the power of knowledge, the gorgeous light of life.

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: 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.