Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Feb 202012
 

Split on specific characters on Bash Script's for loop

Answer:

If you want to Bash script's for loop to split on non white-space characters, you can change the IFS, e.g.

#!/bin/bash
IFS='-'

list="a-b-c-d-e"

for c in $list; do
    echo $c
done

The above script will split on the character '-'

E.g.

# ./test.sh

a
b
c
d
e

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>