how to get elements from list in bash?

no spaces between equal sign

$ List="abcd 1234 jvm something"
$ set -- $List
$ echo $2
1234

Some other ways, although not as efficient as using shell's internals

$ echo $List | cut -d" " -f2
1234
$  echo $List | awk '{print $2}'
1234
$ echo $List | sed 's/^.[^ \t]* //;s/ .*//'
1234
$ echo $List | tr " " "\n"|sed -n '2p'
1234

Just to supplement ghostdog's answer: you could also put $List's elements into an array and access the specific list element from there

List="abcd 1234 jvm something"
arr=($List)
echo ${arr[1]}

Note that the array indices are counted 0,1,2,... .

This has the advantage of not polluting the shell environment with too many new variables.

Tags:

Linux

Bash