Play change volume, environment variables, and math operations (shell)

Source: Internet
Author: User
Tags arithmetic decimal precision scale square root

variables and environment variables

Var=value assigns a value to the variable, the output statement: $ echo $var or $ echo ${var}, remember that there is a space in the middle

For example: Name= "Coffee" age= "$echo" my name is : ${name}, The age is : ${age} " in the output statement Echo You can use a variable in double quotes , But the variable does not extend the defined value if it is a single quotation mark.

Gets the length of the string: ${#var}

1 // /For example:2 name="yy"$echo ${#name} 3 //The result of the output is 2.
View Code

or $SHELL can get the kind of shell that's currently in use , note:Shell all letters must be in uppercase

The Export command sets the environment variable, after which the current Shell script inherits the value of the variable .

Some common environment variables:HOME PWD USER UID SHELL

UID: Check current script user identity,root user uid is 0

Shell: The type of the current shell

Mathematical Operations

the Let command is a simple arithmetic operation that can be used without adding $ before the variable name

For example:

Self-add operation:$ let one++

Self-subtraction:$ let two--

It is also possible to abbreviate:$ let one+=2 $ let two-=1 respectively equal to $ let one = one + 2 and $ let two = Two-1

The operator [] also has a function similar to the Let command

You can also use (()), but note that you need to add $before the variable name, or you will get an error.

The above method is only suitable for operations between integers, and does not support decimals.

Real life without decimals that is almost impossible, of course, the Shell is not so simple,theBC command is a high-level mathematical arithmetic tool that performs floating-point arithmetic and applies some advanced functions:

Set decimal Precision scale command, no effect on multiplication (*) operation

Convert obase ( output binary ) and ibase ( input-to- binary ) commands between binary

Computes the square, square root sqrt command

Note: If you do not have the square root, then the result is the maximum value that is smaller than the original value.

Play change volume, environment variables, and math operations (shell)

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.