bash loop through all chars in string [duplicate] - bash

This question already has answers here:
How to perform a for loop on each character in a string in Bash?
(16 answers)
Closed 7 years ago.
How should I loop through all chars in a string.
My pseudocode
stringVar="abcde"
for var in stringvar
{
do some things with var
}
result i need
a
b
c
d
I want to loop all the vars but i can only get it to work with a whitespace splitted var like
stringVar="a b c"
for var in stringVar; do
echo $var
done;
result
a
b
c
but i cant do it for a string that isnt split with whitespaces.
The question is flagged as duplicate but not one of the answers (with upvotes) is available in the linked questions..

You can use read for that:
string="abcde"
while read -n 1 char ; do
echo "$char"
done <<< "$string"
Using -n 1 read will read one character at a time. However, the input redirection <<< adds a newline to the end of $string.

stringVar="abcde"
for ((i=1;i<=${#stringVar};i++)); do
echo ${stringVar:$i-1:1}
done
Output:
a
b
c
d
e

Related

Populate bash array from a string containing escaped space [duplicate]

This question already has answers here:
Reading quoted/escaped arguments correctly from a string
(4 answers)
Honoring quotes while reading shell arguments from a file
(1 answer)
Closed 4 years ago.
I want to populate a bash array by splitting one of the arguments based on the space but preserving the escaped space or double quotes.
populate_array() {
params="${1}"
array=(-j $params)
for elem in "${array[#]}"
do
echo "${elem}"
done
}
cols="x\ y"
populate_array "${cols}"
Output:
-j
x\
y
Desired output:
-j
x y
I even tried escaped double quotes
populate_array() {
params="${1}"
array=(-j $params)
for elem in "${array[#]}"
do
echo "${elem}"
done
}
cols="\"x y\""
populate_array "${cols}"
Output:
-j
"x
y"
Desired output:
-j
x y
FYI it can be easily done using eval, but I'd rather prefer not to do that.
The answers https://stackoverflow.com/a/31485948/3086551 explain by either taking human generated input or reading from the file. I want to parse the passed argument with escaped space or double quotes instead.

How to understand variable and variable's value in bash [duplicate]

This question already has answers here:
How to get a variable value if variable name is stored as string?
(10 answers)
Closed 5 years ago.
#!/bin/bash
list="one two three"
one=1
two=2
three=3
for k in $list
do
echo $k
done
For the code above, output is:
one
two
three
But I always think it should output:
1
2
3
It's confusing. How to understand this?
The expansion $k just gives you the name of the variable as a string. If you want the value of the variable, you must use the parameter expansion syntax ${!k}. This is documented here.
#!/bin/bash
list="one two three"
one=1
two=2
three=3
for k in $list
do
echo "${!k}"
done
Output
1
2
3

BASH: "while read line ???"

I understand the format below...
while read line
do
etc...
However, I saw this yesterday and haven't been able to figure out what var would be in the following:
while read pkg var
do
etc...
Thanks
while loop will read the var one by one , but assign the last parts to one var.
For example, I have a file like:
a b c d
when run the command
$ while read x y
do
echo $x
echo $y
done < file
Resule:
a
b c d
You will get "b c d" to $y.
Of course, if you only assign one var (line), then $line will get the whole line.
The read builtin will read multiple whitespace-separated (or, really, separated by whatever is in $IFS) values.
echo a b c | (read x y z; echo "$y")
#=> b
If there are more fields than variables passed to read, the last variable gets the rest of the line.

How can I get the first string from a Bash list? [duplicate]

This question already has answers here:
How can I retrieve the first word of the output of a command in Bash?
(13 answers)
Closed 1 year ago.
I do have a Bash list (space separated string) and I just want to extract the first string from it.
Example:
VAR="aaa bbb ccc" -> I need "aaa"
VAR="xxx" -> I need "xxx"
Is there another trick than using a for with break?
Use cut:
echo $VAR | cut --delimiter " " --fields 1 # Number after fields is the
# index of pattern you are retrieving
Try this format:
echo "${VAR%% *}"
Another way is:
read FIRST __ <<< "$VAR"
echo "$FIRST"
If you want arrays, use arrays. ;)
VAR=(aaa bbb ccc)
echo ${VAR[0]} # -> aaa
echo ${VAR[1]} # -> bbb
I'm not sure how standard this is, but this works in Bash 4.1.11
NewVAR=($VAR)
echo $NewVAR
At this moment the only solution that worked, on both Linux and OS X was:
IP="1 2 3"
for IP in $IP:
do
break
done

bash: read string into array bash with spaces preserved [duplicate]

This question already has answers here:
Bash: Split string into character array
(20 answers)
Closed 9 years ago.
I have string "hi how are you"
I want to put this string into an array as shown below. But i want to preserve spaces. Any ideas on how to do that?
a[0] a[1] a[2] 3 4 5 6 .... should have
h i <space> h o w <space> .... and so on.
One way, sure there will be better solutions but this seems to work for me:
unset arr; IFS=; for c in $(sed 's/./&\n/g' <<<"hi how are you"); do arr+=("$c"); done; echo "${arr[#]}"
It yields:
h
i
h
o
w
a
r
e
y
o
u
eval a=( $(echo "hi how are you" | sed "s/\(.\)/'\1' /g") )
It's really ugly, maybe somebody can come up with something without eval...
Probably not fast, but avoids the need for sed:
z=()
while read -n 1 x; do
z+=( "$x" )
done <<<"hi how are you"

Resources