BASH快速參考

 

1. 註釋

Example:
    # This is a comment

2. 轉義字符

Example:
    rm *
    ls ??
    cat file[1-3]
    echo "How are you?"

3. 輸出

Example:
    echo "Hello world"

3. 局部變量

Example:
    variable_name=value
    declare variable_name=value
    name="John Doe"
    x=5
注:等號兩邊不能有空格存在

4. 全局變量

Example:
    export VARIABLE_NAME=value
    declare -x VARIABLE_NAME=value
    export PATH=/bin:/usr/bin:.

5. 變量輸出

Example:
    echo $variable_name
    echo $name
    echo $PATH

6. 輸入

Example:
    read variable_name
    read name1 name2 ...

7. 參數

Example:
    $scriptename arg1 arg2 arg3
    在腳本內可以使用如下方式訪問參數
    echo $1 $2 $3    輸出參數arg1, arg2, arg3
    echo $*             輸出所有參數
    echo $#             輸出參數個數

8. 數組

Example:
    declare -a array_name=(world1 world2 world3 ...)
    declare -a fruit=(apples pears plums)
    echo ${fruit[0]}

9. 命令

 Example:
    variable_name=`command`
    variable_name=$(command)

10. 算術操作

Example:
    declare -i variable_name
    typeset -i variable_name
    ((n=5+5))
    echo $n

11. test命令操作符

    ==
    !=
    >
    >=
    <
    <=

12. if條件語句

Example:

    if command
    then
        block of statements
    fi

    if command
    then
        block of statements
    elif command
    then
        block of statements
    fi

    if [[ expression ]]
    then
        block of statements
    fi

    if (( numeric expression ))
    then
        block of statements
    else
        block of statements
    fi

13. case語句

Example:
    case variable_name in
        pattern1)
            statements
            ;;
        pattern2)
            statements
            ;;
        pattern3)
            statements
            ;;
    esac

14. 循環

Example:
    while command
    do
        block of statements
    done

    until command
    do
        block of statements
    done

    while [[ string expression ]]
    do
        block of statements
    done

    until [[ string expression ]]
    do
        block of statements
    done

    for variable in word_list
    do
        block of statements
    done

    select variable in word_list
    do
        block of statement
    done

15. 函數

Example:
    function_name() {
        block of code
    }

    function function_name {
        block of code
    }


=== end ===
發佈了71 篇原創文章 · 獲贊 6 · 訪問量 25萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章