1. 程式人生 > >Shell中的expr命令

Shell中的expr命令

expr EXPRESSION

EXPRESSION的值列印到標準輸出

  • 使用index命令
test="AaBbCcDdEeFfGg"

echo $(expr index $test A)
echo $(expr index $test D)
echo $(expr index $test Z)

  • 使用substr命令
echo $(expr substr $test 2 5)

  • 使用match命令
#test not start with a, return 0
echo $(expr match $test "a*")
#test start with A, return 1
echo $(expr match $test "A*")
#test end with g, return matched position 14
echo $(expr match $test ".*g$")
#test not end with G, return 0
echo $(expr match $test ".*G$")

echo $(expr match $test "\(.*g$\)")
echo $(expr match $test "\(A*\)")

程式碼段

/bin/bash

test="AaBbCcDdEeFfGg"

echo $(expr index $test A)
echo $(expr index $test D)
echo $(expr index $test Z)


echo $(expr substr $test 2 5)

#test not start with a, return 0
echo $(expr match $test "a*")
#test start with A, return 1
echo $(expr match $test "A*")
#test end with g, return matched position 14
echo $(expr match $test ".*g$")
#test not end with G, return 0
echo $(expr match $test ".*G$")

echo $(expr match $test "\(.*g$\)")
echo $(expr match $test "\(A*\)")

echo $(expr length $test)

echo $(expr 5 = 5)
echo "bye..."

執行結果
在這裡插入圖片描述