ShravanM
ShravanM

Reputation: 323

split or cut and assign values of a variable to another variable in unix

My variable has value "test one two", i want them to be separated by " " and store in different variable.

#/usr/bin/ksh


var="test one two"
___________________________ command
    temp1 = test
    temp2 = one
    temp3 = two

Upvotes: 1

Views: 1505

Answers (2)

Ed Morton
Ed Morton

Reputation: 203792

Assuming you can use bash since you tagged your question with bash, this is probably what you really should do:

$ var="test one two"
$ temp=( $var )
$ echo "${temp[0]}"
test
$ echo "${temp[1]}"
one
$ echo "${temp[2]}"
two

but I suspect whatever script you're using this in is probably employing the wrong approach to solve whatever your larger problem is.

Upvotes: 2

anubhava
anubhava

Reputation: 785376

In ksh you can use read:

var="test one two"
echo "$var" | read temp1 temp2 temp3
$ echo "$temp1"
test
$ echo "$temp2"
one
$ echo "$temp3"
two

Upvotes: 0

Related Questions