Hi all,

I have a problem I always though i knew the answer to.

I have a variable with spaces and I want to perform an operation on each.

VAR="THIS" "THAT" "THIS AND THAT" "THIS AND THIS"

for i in $VAR
do
echo "$i"
done

I thought that I should be able to use $i in speech marks for this "$1" but it doesn't work and I don't seem to be able to suss it out.

Any ideas?

thanks

Recommended Answers

VAR="THIS" "THAT" "THIS AND THAT" "THIS AND THIS"

You can not assign variables as such. However you can VAR="THIS THAT THIS AND THAT THIS AND THIS" Nevertheless, I feel you probably do not want that since it can not make a distinction for "sentences" like "THIS AND THAT"

Jump to Post

All 2 Replies

VAR="THIS" "THAT" "THIS AND THAT" "THIS AND THIS"

You can not assign variables as such. However you can VAR="THIS THAT THIS AND THAT THIS AND THIS" Nevertheless, I feel you probably do not want that since it can not make a distinction for "sentences" like "THIS AND THAT"

Maybe what you want is an array of sentences.

var=( "THIS" "THAT" "THIS AND THAT" "THIS AND THIS" )

for i in "${var[@]}"; do
    echo $i
done
var='"THIS" "THAT" "THIS AND THAT" "THIS AND THIS"'

eval "set -- $var"

for i
do
  echo $i
done
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.