計算
    Top > Linux > sh
    Four arithmetic operations of integer
    整数の四則演算
    Syntax: $((equation)) Only integer is allowed. +, -, * and % can be used as operator.
    Equation Result
    A + B A + B
    A - B A - B
    A * B A * B
    A / B Quotient of A / B
    A % B Remainder of A / B
    expr
    Four arithmetic operations of integer 整数の四則演算
    Equation Return Note
    A + B A + B
    A - B A - B
    A \* B A * B Put a backslash in front of '*'.
    A / B Quotient of A / B
    A % B Remainder of A / B
    式 戻り値 備考
    A + B A + B
    A - B A - B
    A \* B A * B '*'の前にバックスラッシュを置く.
    A / B A / B の商
    A % B A / B の余り
    Parenthesis also needs a backslash as follows. 括弧も前にバックスラッシュが必要. Evaluation of equality or inequality 等式・不等式の評価 expr returns 1 if conditional expression is true, and exit code is 0. If false it returns 0 and exit code is 1. exprは条件式が真のとき1を返し,終了コードは0である. 偽のときは0を返し,終了コードは1である. On the following table, A and B are integers, and P and Q are conditional expressions. 次の表でAとBは整数,PとQは条件式である.
    Conditional expression Meanings Note
    A = B A is equal to B.
    A != B A is not equal to B.
    A \< B A is less than B. Put a backslash in front of '<'.
    A \<= B A is less than or equal to B. "
    A \> B A is greater than B. Put a backslash in front of '<'.
    A \>= B A is greater than or equal to B. "
    P \& Q Both P and Q is true. Put a backslash in front of '&'.
    P | Q Either P or Q is true. Put a backslash in front of '|'.
    条件式 意味 備考
    A = B AはBに等しい.
    A != B AはBに等しくない.
    A \< B AはBより小さい. バックスラッシュを'<'の前に置く.
    A \<= B AはBより小さいないしBに等しい.       〃
    A \> B AはBより大きい. バックスラッシュを'>'の前に置く.
    A \>= B AはBより大きいないしBに等しい.       〃
    P \& Q PとQはともに真. バックスラッシュを'&'の前に置く.
    P | Q PとQのどちらかが真. バックスラッシュを'|'の前に置く.
    expr returns exit code, so it can be used in if statement. exprは終了コードを返すので,if文で使うことが出来る. Output: As shown in the first example of expr, the return 0 or 1 is printed. Therefore, output it to /dev/null when using in if statement. exprの最初の例で示したように,戻り値の0か1がプリントされる.従ってこれをif文で使う際は/dev/nullに出力する.
    References
    【 整数計算を行う「expr」 】 | 日経クロステック(xTECH) + https://xtech.nikkei.com/it/article/COLUMN/20060228/231170/