linux – 如何在bash中提取一个子字符串

前端之家收集整理的这篇文章主要介绍了linux – 如何在bash中提取一个子字符串前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在bash中有以下字符串,长度> 4
str = "abcdefghijklmno"

我想提取到str2的第一个第一个char的字符串.所以

str2="abcde"

如何用bash?

解决方法

请使用表达式
{string:position:length}

所以在这种情况下

$str="abcdefghijklm"
$echo "${str:0:5}"
abcde

查看其他用法

$echo "${str:0}"      # default: start from the 0th position
abcdefghijklm
$echo "${str:1:5}"    # start from the 1th and get 5 characters
bcdef
$echo "${str:10:1}"   # start from 10th just one character
k
$echo "${str:5}"      # start from 5th until the end
fghijklm

取自:
wooledge.org – How can I use parameter expansion? How can I get substrings? How can I get a file without its extension,or get just a file’s extension?
Shell Command Language – 2.6.2 Parameter Expansion

原文链接:https://www.f2er.com/linux/393493.html

猜你在找的Linux相关文章