0

i am trying to use the loop variable within the loop. But it seems that it is loosing its value after using one time(may be i am misunderstanding). Here is my code

for i in {2000..2014}
do
    echo “training  representation$i_by_$i and testing representation$1999_by_$i”
done

I want the output should be like this-

training representation2000_by_2000 and testing representation1999_by_2000

but the actual output is like this-

training representation2000 and testing representation1999_by_??

can any body help?

2
  • is your $1 actually 1? Commented Feb 5, 2016 at 22:00
  • Obviously the value isn't being lost, or you wouldn't have any 2000s in your output. Commented Feb 5, 2016 at 22:04

2 Answers 2

2

Because you're using curly quotes around your string. The character is not a special character to the shell, so it's being treated as part of the variable name. But there's no variable named i”.

Change to ASCII double quotes around the string. Also, since _ can be in a variable name, you need to use ${i} when the variable is followed by that character (or any other character that can be in a variable name); otherwise, it's looking for a variable named $i_by_.

Finally, $1999 is being interpreted as $1 (the first argument to the script)followed by999. To prevent that, you need to escape the$`.

for i in {2000..2014}
do
    echo "training  representation${i}_by_$i and testing representation\$1999_by_$i"
done

And stop using a word processor to edit programs. Use a programming editor, it won't auto-correct your quoting.

Sign up to request clarification or add additional context in comments.

9 Comments

That and $i_by_ is not the same as $i.
Can a variable name contain special characters? I doubt that.
@KarolyHorvath, it absolutely can contain underscores.
@KarolyHorvath, I was reading you as responding to Benjamin rather than to the first sentence of Barmar's answer, so I actually misread your point.
@Barmer your ans does not giving me the expected ans by the way i found me expected ans by myself but thanks anyway. "training representation${i}_by_${i} and testing representation1999_by_${i}" And the person gave me a downvote can you please share the reason. I want to learn. Thanks
|
2

Well, you're missing some quotes, you can use that kind of quotes, but when you add _ after calling the variable $i, bash thinks you're calling another variable called $i_by_.

Try:

for i in {2000..2014}
do
    echo "training  representation"$i"_by_"$i"and testing representation1999_by_$i"
done

With that you're printing 5 objects, training representation, $i,_by_,$i and and testing representation1999_by_$i

Getting the output you need:

“training representation2000_by_2000and testing representation1999_by_2000”

Also, at the end you had $1999, i believe by mistake.

EDIT: As suggested at the comments, you can also use:

echo "training representation${i}_by_${i}and testing representation1999_by_${i}"

To use the variables inside the quote.

@Barmar: Those quotes are fine, the problem is not there; i tried with both and worked the same way.

3 Comments

Expanding variables unquoted is very bad form. Look at what happens if your code is run with IFS=1234567890. Use ${i} inside quotes to disambiguate string concatenation; don't end quotes before an expansion and thus subject that expansion to string-splitting (as by characters in IFS) and globbing.
That's another way, i know, but if the iteration variable is well know with regular values, shouldn't be a problem.
As I said above (and gave an example for demonstration purposes), even if the value expanded contains only known characters, an irregular value for IFS can still cause problems as above.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.