Method 1: Split string using read command in Bash Here’s my sample script for splitting the stringusing read command: #!/bin/bash # # Script to split a string based on the delimiter my_string="Ubuntu;Linux Mint;Debian;Arch;Fedora" IFS=';' read -ra my_array <<< "$my_string" #Pr...
Split string by delimiter IFS is needed even though you are not going to use it. The string gets split into the tokens array variable. IFS=':' read -a tokens <<< "foo:bar:baz" echo ${tokens[0]} #prints "foo" some_var=${tokens[2]} echo $some_var #prints "baz" Felipe...
在这个函数的底部,你会发现一个函数,它可以将string转换为一个array,语法如下:
在这个函数的底部,你会发现一个函数,它可以将string转换为一个array,语法如下:
split3.sh #!/bin/bash #Read the main string echo "Enter the string with colon(:) to split" read mainstr #Split the string based on the delimiter, ':' readarray -d : -t strarr <<< "$mainstr" printf "\n" # Print each value of the array by using loop for (( n=0; n <...
/bin/bash#Read the main stringtext="Welcome:to:GeeksforGeeks"#Split the string based on the delimiter,':'readarray-d:-t strarr<<<"$text"#Print each value of the array byusing#loopfor((n=0;n<${#strarr[*]};n++))doecho"${strarr[n]}"done...
Before I tell you how it works, know thatthe result will be an array that contains split strings. :) With that, let's take a look at an example! #!/usr/bin/env bash my_string='Hello world' IFS=' ' read -ra my_array <<< "${my_string}" ...
我在循环中打印它时只得到第一个字符串,没有括号围绕$IN它起作用。 您可以设置内部字段分隔符(IFS)变量,然后将其解析为数组。当在命令中发生这种情况时,对IFS的分配仅发生在该单个命令的环境中(要read)。然后它根据IFS变量值将输入解析为一个数组,然后我们可以迭代它。
The output shows the first element of theip_array. The script below uses thecutcommand to extract a substring. The-doption specifies the delimiter to use to divide the string into fields and the-foption sets the number of the field to extract. ...
BASH_EXECUTION_STRING The command argument to the -c invocation option. BASH_LINENO An array variable whose members are the line numbers in source files corresponding to each member of FUNCNAME. ${BASH_LINENO[$i]} is the line number in the source file where ${FUNCNAME[$i]} was called ...