How to extract the first two characters of a string in shell scripting?

前端 未结 14 2109
-上瘾入骨i
-上瘾入骨i 2020-12-07 13:54

For example, given:

USCAGoleta9311734.5021-120.1287855805

I want to extract just:

US
14条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-07 14:09

    If your system is using a different shell (not bash), but your system has bash, then you can still use the inherent string manipulation of bash by invoking bash with a variable:

    strEcho='echo ${str:0:2}' # '${str:2}' if you want to skip the first two characters and keep the rest
    bash -c "str=\"$strFull\";$strEcho;"
    

提交回复
热议问题