Bash: How to tokenize a string variable?

前端 未结 5 688
眼角桃花
眼角桃花 2020-12-08 13:33

If I have a string variable who\'s value is \"john is 17 years old\" how do I tokenize this using spaces as the delimeter? Would I use awk?

5条回答
  •  盖世英雄少女心
    2020-12-08 14:16

    you can try something like this :

    #!/bin/bash
    n=0
    a=/home/file.txt
    for i in `cat ${a} | tr ' ' '\n'` ; do
       str=${str},${i}
       let n=$n+1
       var=`echo "var${n}"`
       echo $var is ... ${i}
    done
    

提交回复
热议问题