How can I sort the string array in linux bash shel

2019-06-26 11:07发布

This question already has an answer here:

For instance, the array is

link2_pathname
link1_pathname
link3_pathname

How can I get the array like below.

link1_pathname
link2_pathname
link3_pathname

Thanks a lot in advance!

标签: linux bash shell
2条回答
我只想做你的唯一
2楼-- · 2019-06-26 11:18

try this

var=( link2_pathname link1_pathname link3_pathname )

for arr in "${var[@]}"
do
    echo $arr
done | sort

new_var=( $(for arr in "${var[@]}" 
do
        echo $arr
done | sort) )
查看更多
冷血范
3楼-- · 2019-06-26 11:44

pipe a loop to sort.

a=(l2 l3 l1)
b=($(for l in ${a[@]}; do echo $l; done | sort))

you probably need to watch out for the IFS when handling string values containing spaces.

查看更多
登录 后发表回答