Glam Prestige Journal

Bright entertainment trends with youth appeal.

I wanted two print to variables in one line. I am using a shell script #!/bin/sh loop and what I wanted to do is a for repeat which prints out something like:

variable1_case1
variable2_case2

and I have already tried

variable$i_case$i.
1

3 Answers

for i in 1 2; do echo variable${i}_case$i
done

should do what you want. Substitute 1 2 with the numbers or strings you need. Dependent on the values of $i you may need to quote it like so: echo variable"$i"_case"$i".

0

I assume the problem in your script is that you try to embed the variable names directly inside a text string, so that they are followed by other characters which could be part of a variable name. These do not only include alphanumeric characters but also the underscore.

So if you want to embed variables into a string in a way so that they are not separated from the rest by spaces or any non-variable-name characters, you can use the variable name notation with curly braces instead:

$ i=42
$ echo "variable${i}_case${i}."
variable42_case42.
5

Maybe I have gotten the wrong end of the stick, but do you want something like this:

#!/bin/sh
while read a b; do echo variable:${a}_case:${b}
done

which will create this output:

/tmp/test.sh < /tmp/dat1
variable:1_case:2
variable:3_case:4

and the data file dat1 containing:

1 2
3 4

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy