Linux Shell variables

Source: Internet
Author: User
Tags arithmetic

Introduction

Variables are an essential part of Shell scripting, and using variables in scripts does not need to be declared in advance. Each variable in bash is a string, so whenever a variable is assigned a value, whether or not the quotation marks are stored as a string, but if a special character exists in the value you need to quote the value in quotation marks, you can use single or double quotes in bash.

Environment: CentOS 6.7

Assign value

Assign a value to a variable:

Var=value

Pay particular attention to the absence of a space between the variable and the value, otherwise it will be considered an equivalent judgment, e.g. [var = value]

Calling a variable only needs to precede the variable with the $

Output variables:

echo $value or Echo ${value}

Example: Write a shell script output today's date

Vim Assign

# !/bin/bashvar= ' date +'%y-%m-%d'"today is $var  "

Operation

[]、(())

Note that the two above are fixed notation, are used to do arithmetic operations in the shell, and before both operators need to use the $ match, in parentheses before the variable can be used or can not use

# !/bin/bashvar1=1var2=2result1=$[var1+var2]result2 =$[  $result 1+1]result3=$ ((result2+1)) Result4=$ (($result 3 +1$result 1$result 2$result 3$result 4

In a shell environment, you can use let,[] and (()) to perform basic arithmetic operations.

Let

Let's arithmetic operations include, allow itself can also be assigned

Method 1: Simple arithmetic operations

var1=1var2=2Letresult=var1+var2

Method 2: Self-add

Let var2++ equates to let var2=var2+1

Method 3: Shorthand

Let var2+=2 equates to let Var2=var2+2

Script

#!/bin/bashvar1=1var2=2Let result=var1+Var2let var2++Let var1+=5printf "var1=1 var2=2 \ n"printf  "%-10s%-8s%-6s\n"Var1+var2 var2++ var1+=5printf  "%-10s%-8s%-2s \ n" $result $var 2 $var 1Let var=5Echo$var

Note: The above only tests the addition operation, which is supported for other arithmetic operations, but only supports shaping, and is not supported for operations with decimals.

Advanced Operations BC

Supports decimal operations

Grammar
"  " | Bc

If you want to use the result of a BC operation as a variable, you can

" " | BC '
# !/bin/bash " 0.5*10 " | Bcvar=5result"$var *0.5" |  $result

Summary

The assignment algorithm is still relatively simple, the latter is gradually perfected

Note:

pursuer.chen

Blog:http://www.cnblogs.com/chenmh

This site all the essays are original, welcome to reprint, but reprint must indicate the source of the article, and at the beginning of the article clearly give the link.

Welcome to the exchange of discussions

Linux Shell variables

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.