Fundamentals of Linux Foundation--bash programming

Source: Internet
Author: User
Tags arithmetic arithmetic operators

Does the bash test have a grammar test

Bash-n file.sh

Script execution in debug mode

Bash-x file.sh


Defining global variables in the shell

Export date= ' DATE +%y%m%d '


Arithmetic operators in the shell

A=3

B=4

If you use c= $A + $B to represent the addition of strings, the result is 3+4

1. Let arithmetic operation expression

Let c= $A + $B

2. $[arithmetic operation expression]

c=$[$A + $B]

3, $ ((arithmetic operator))

c=$ (($A + $B))

4. Expr arithmetic operator, with spaces between expressions, and using command references

c= ' expr $A + $B '


Conditional test in Shell

1, integer test (binocular operation, compare two numbers, usually variables and numbers or variables and variables)

expression [$A-eq $B] (command test method, note space)

[[$A-eq $B]] (keyword test method)

Test $A-eq $B (Tests command testing method)

-GT greater than

-lt less than or equal to

-ne Not equal to

-eq equals

-ge greater than or equal to

-lt less than

Note: There is no need to use [] in addition to the above-mentioned judgment when testing.

such as: if grep "^ $USERNAME \>"/etc/passwd; then can be directly judged without using []


2. File test

-e file: Test files exist

-F File: Test files are normal files

-D File: Test whether the files are directories

-R File: Tests whether the current user has read access to the specified file

-W File: Tests whether the current user has write access to the specified file

-X file: Tests whether the current user has execute permissions on the specified file

such as: if [-f/etc/inittab] (note space)


Exit # define Exit status code


Bash Variable type

Local variable (local variable) "Scope is current shell"

Environment variable "Scope is current shell and child shell"

Positional variables

$ $

Shift N (kicks the first n position variables)

Special variables

$?: previous command execution status

$#: Number of parameters

$*: Parameter list

[email protected]: parameter list


This article from "Liang Childe" blog, please be sure to keep this source http://iyull.blog.51cto.com/4664834/1884528

Fundamentals of Linux Foundation--bash programming

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.