【shell腳本】字符串和數組的使用

  字符串數組

能夠使用單引號和雙引號定義字符串變量可是單引號中不支持變量解析bash

#! /bin/bash
username="mayuan" str_1="hello ${username}" str_2='hello ${username}' echo $str_1 # hello mayuan echo $str_2 # hello ${username}

獲取字符串的長度spa

#! /bin/bash
username="mayuan" echo ${#username} # 6

截取字符串code

#! /bin/bash
username="mayuan" echo ${username:1:3} # 從第二個字符開始截取3個字符輸出"ayu"

查找指定字符blog

#! /bin/bash
username="mayuan"
echo $(expr index "${username}" y)  #查找y在指定字符的位置

  數組字符串

#! /bin/bash
arr=(1 2 3 4 5)
echo ${arr[0]} #輸出1
echo ${arr[1]} #輸出2

獲取數組長度class

#! /bin/bash
arr=(1 2 3 4 5)
echo ${#arr[@]} #輸出5
echo ${#arr[*]}
相關文章
相關標籤/搜索