Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace blank spaces in an array in bash

I have to replace blank spaces (" ") of all the values from an array in shell script. So, I need to make an array like this:

$ array[0]="one"
$ array[1]="two three"
$ array[2]="four five"

looks like this:

$ array[0]="one"
$ array[1]="two!three"
$ array[2]="four!five"

Replacing every blank space to another character with a loop or something, not changing value by value.

like image 402
user3754968 Avatar asked Feb 13 '26 01:02

user3754968


1 Answers

array=('one' 'two three' 'four five') # initial assignment
array=( "${array[@]// /_}" )          # perform expansion on all array members at once
printf '%s\n' "${array[@]}"           # print result
like image 143
Charles Duffy Avatar answered Feb 14 '26 21:02

Charles Duffy