I have the following string in bash with length > 4
我在bash中有以下字符串,长度为>4
str = "abcdefghijklmno"
and I want to extract into str2
the 5 first charachter of str
. So
我想提取str2, str的第5个特征
str2="abcde"
How to do it with bash?
如何使用bash?
1 个解决方案
#1
27
Do use the expression
使用表达式
{string:position:length}
So in this case:
所以在这种情况下:
$ str="abcdefghijklm"
$ echo "${str:0:5}"
abcde
See other usages:
看到其他的用法:
$ 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
Taken from:
- 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
引用自:- wooledge.org -我如何使用参数扩展?如何得到子字符串?如何获得一个没有扩展名的文件,或者仅仅获得一个文件的扩展名?- Shell命令语言- 2.6.2参数扩展
#1
27
Do use the expression
使用表达式
{string:position:length}
So in this case:
所以在这种情况下:
$ str="abcdefghijklm"
$ echo "${str:0:5}"
abcde
See other usages:
看到其他的用法:
$ 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
Taken from:
- 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
引用自:- wooledge.org -我如何使用参数扩展?如何得到子字符串?如何获得一个没有扩展名的文件,或者仅仅获得一个文件的扩展名?- Shell命令语言- 2.6.2参数扩展