Krin123
Krin123

Reputation: 343

splitting a string into individual characters in bash using IFS

I'm trying to split a string into individual characters. For example temp="hello" into "h", "e", "l", "l", "o"

I tried using IFS because that's what I used in previous string splits and wanted to keep the consistency across the script. IFS='' read h e l l o <<<"$temp" does not work. What am I doing wrong?

Upvotes: 3

Views: 817

Answers (2)

chepner
chepner

Reputation: 531808

TL;DR: this is just to see if it can be done. Use fold like anubhava suggests; starting a single process is a small price to pay to avoid not one, but two uses of eval.


I wouldn't actually use this (I think it's safe, but I wouldn't swear to it, and boy, is it ugly!), but you can use eval, brace expansion, and substring parameter expansion to accomplish this.

$ temp=hello
$ arr=( $(eval echo $(eval echo \\\${temp:{0..${#temp}}:1})) )
$ printf '%s\n' "${arr[@]}"
h
e
l
l
o

How does this work? Very delicately. First, the shell expands ${#temp} to the length of the variable whose contents we want to split. Next, the inner eval turns the string \${temp:{0..5}:1} into a set of strings ${temp:0:1}, ${temp:1:1}, etc. The outer eval then performs the parameter expansions that produce one letter each from temp, and those letters provide the contents of the array.

Upvotes: 2

anubhava
anubhava

Reputation: 785561

You can use fold:

arr=($(fold -w1 <<< "$temp"))

Verify:

declare -p arr
declare -a arr='([0]="h" [1]="e" [2]="l" [3]="l" [4]="o")'

Upvotes: 6

Related Questions