Five ways to assign variables to the "Linux" shell programming

Source: Internet
Author: User

1. Types of variables in the shell

Variables in the shell can be divided into environment variables, positional variables, predefined special variables, and user-defined variables,

<!--[if!supportlists]-->2. <!--[endif]--> Environment variables

Environment variables in the shell are a class of shell predefined variables, which are variables used to set the environment of the system, and the environment variables are named by the system uniformly. The values of some system variables are set by the system, and the values of some environment variables can be given by the user.

The name of the environment variable is made up of uppercase letters, and the common shell environment variables are as follows:

Home: Switch to the user's home directory using the full pathname of the owner's directory, CD $HOME

Path: Similar to the path under Windows, the shell looks for the binary executable file in turn.

Echo $PATH can display the current path, add a new path method $PATH = $PATH: New path

Term: terminal type echo $TERM

PS1: prompt, root user defaults to #, normal user defaults to $

Absolute path to the Shell:shell interpreter

LOGNAME: User name of the logged-on user

UID: The UID echo $UID of the current user

<!--[if!supportlists]-->3. <!--[endif]--> position variable

Positional variables are variables that are determined based on the position of the arguments that appear on the command line, and the location of the parameters is defined as shown in the command line that invokes the shell program.

$ command Parameter 1 parameter 2 parameter 3

Where $ A corresponds to the execution of the command name

corresponding parameter 1

$2 for the corresponding parameter

$3 for the corresponding parameter

( be sure to figure out the order!!) )

<!--[if!supportlists]-->4. <!--[endif]--> pre-defined special variables

A predefined special variable has a special meaning that the user cannot change, and all predefined variables are made up of the "$" symbol and another symbol, and the commonly used predefined special variables are as follows

$#: Number of positional parameters (not including shell script name)

$*: A string consisting of positional parameters

$!: The process number corresponding to the previous background command

$?: The exit status of the previous command, which is a decimal number, if returned as 0, represents a successful execution.

$$: Current process number PID

<!--[if!supportlists]-->5. <!--[endif]--> user-defined variables ( case sensitive )

Requirement: The variable name begins with a letter or underscore followed by any number of letters, numbers, and underscores.

Legal:

_name SIZE My_idea

Illegal:

2god!name Hello&world

<!--[if!supportlists]-->6. <!--creation of [endif]--> variables

There are two built-in commands declare and typeset can be used to create variables. You can also set the creator of a variable through the option settings of the command.

In addition to using built-in commands to create and set variables, you can also assign values directly, in the following format:

Variable name = variable Value

Note: The variable name should not precede the dollar "$" symbol. (Different from PHP)

There can be no spaces before or after the equals sign "=".

Unlike the C language, explicit syntax is not required in the shell to declare variables.

The variable name cannot be directly connected to other characters, and if you want to connect it, you must use parentheses: echo "This is $ (he) llo!"

<!--[if!supportlists]-->7. <!--deletion of [endif]--> variables

The system provides the unset command to delete variables, such as

unset name

<!--[if!supportlists]-->8. <!--assignment of [endif]--> variables

There are five types of variables: Use the read command to assign values directly, use command-line arguments, and use the command-line output to read from a file.

Let's start with the Read command: ( mostly when you need to interact )

The read command is a system built-in command in the syntax format:

Read Variable 1 variable 2

When the shell script executes to the read command, it pauses execution of the script and waits for keyboard input, and when the user finishes typing and knocks down the Enter, the assignment is completed and the script resumes execution.

Rules of the Read directive:

<!--[if!supportlists]-->l <!--[endif]--> multiple data or variables separated by a space

<!--[if!supportlists]-->l <!--[endif]--> If the number of variables is equal to the number of data, the corresponding value

<!--[if!supportlists]-->l <!--[endif]--> If the number of variables is greater than the number of data, the variable with no input data takes a null value

<!--[if!supportlists]-->l <!--[endif]--> If the number of variables is less than the number of data, the extra data is assigned to the last variable (that is, the space is treated as a string)

Example:

#!/bin/bash

Echo–n "Please enter your name"//-n indicates user input and prompt information on the same line

Read name

echo "Your name is $name"

#!/bin/bash

echo "Please enter your first name and last name:"

Read First Last

echo "Your first name is $first"

echo "Your last name is $last"

The second assignment method is to assign a value directly to the variable ( which is mainly used when no interaction is required and the parameter does not need to be modified )

Example: Name=xiaosu

Gender=man

The third assignment method is to use command-line arguments to assign values. ( This assignment method is that the parameters need to change frequently and do not need to be used when interacting )

This method of assignment, which is followed by the argument directly behind the command, is then used by the system to refer to the first parameter.

The fourth method is to use the output of the command to assign a value ( This method of assignment can directly process the data generated by the previous command )

In a shell program, you can use the output of a command as a variable, but you'll need an inverse quote in an assignment statement

For example currentdir= ' pwd '

Echo $currentdir

The last assignment method is to read the data from the file

This approach is suitable for processing large quantities of data, writing the corresponding data directly to the file, and then running the script.

Typically, a row of data is read through a while loop, that is, a row of data is read from the file until the end of the file is not looped.

For example:

#!/bin/bash

LS *.sh >execfile

While Read line

Do

chmod a+x $LINE

Done<execfile

This script uses the while: Do.. Done structure, and the last line reads the data from the file execfile. While the read line indicates that a row of data is read in each loop and assigned to the variable lines.

<!--[if!supportlists]-->9. <!--output of the [endif]--> variable

The simplest way is to use the Echo

If you want to output a formatted string, you need to use printf, which is similar to the C language

format specifier for the printf command

\ "Output double quotation marks

\ \ Output back slash

\a Bells

\ nthe line break

\ r Enter


Five ways to assign variables to the "Linux" shell programming

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.