如何在bash中提取子字符串 [英] how to extract a substring in bash
本文介绍了如何在bash中提取子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在bash中使用以下字符串,其长度> 4
I have the following string in bash with length > 4
str = "abcdefghijklmno"
,我想将 str
的前五个字符提取到 str2
中.所以
and I want to extract into str2
the 5 first charachter of str
. So
str2="abcde"
如何使用bash进行操作?
How to do it with 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-如何使用参数扩展?如何获得子字符串?如何获取没有扩展名的文件,或者只获取文件的扩展名?
- Shell命令语言-2.6.2参数扩展
这篇关于如何在bash中提取子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文