-1

This question already has an answer here:

I'm trying to write a shell script using bash for the following problem:

Write a loop to go through three values (A B C) and displays each of these values to the screen (hint use a ‘for’ loop).

I figured out it would be something like this but I'm not sure, so any advice would be much appreciated.

For (( EXP1; EXP2; EXP3 ))
do
     command1
     command2
     command3
done

marked as duplicate by Wildcard, Scott, daisy, Archemar, dr01 Apr 8 '16 at 6:55

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • you want value exp1 to print or exp1 exp2 exp3 to execute ? – cutzero Apr 8 '16 at 4:07
  • Thanks for replying! I want the value T exp 1 exp2 exp3 to execute – Josh07T Apr 8 '16 at 4:08
  • @cutzeroLonGdueZBOODacrovinungh, read the problem description again; it is unambiguously a duplicate. The fact that the OP doesn't know the echo command doesn't change that. "...go through three values...display each of these values to the screen..." – Wildcard Apr 8 '16 at 4:20
0
for i in Alpha Beta Charlie; do
  echo "$i"
done

You don't need eval and you don't need ls.

Alternatively, you could just print each of these directly, with a newline character after each:

printf '%s\n' Alpha Beta Charlie

Please don't use eval unless you absolutely must. (Hint: Unless you get seriously complicated, you never need it.) Using eval when you don't have to just leads to security holes.


Your initial question is very clear:

Write a loop to go through three values (A B C) and displays each of these values to the screen (hint use a ‘for’ loop).

However from your comment perhaps you want to execute some commands in a loop:

I want the value T exp 1 exp2 exp3 to execute

If you want to execute commands in a loop, just write the commands and put them in a loop:

for i in {1..3}; do
  echo Hip Hip
  echo 'HOORAY!'
done

The first line of this could just as easily be for i in A B C; do and it would do the same thing. You don't use the variable $i anywhere in your loop, but you don't have to. It will still execute three times.

  • Why the downvotes? – pfnuesel Apr 8 '16 at 4:25
  • 1
    @pfnuesel, I would say strategic downvotes by someone giving a very strange answer. Your answer is perfectly fine; I started out the same but expanded it a little further. – Wildcard Apr 8 '16 at 4:27
  • no strategic, the exp must be executed. – cutzero Apr 8 '16 at 4:30
  • omg i give up... – cutzero Apr 8 '16 at 4:36
  • @cutzeroLonGdueZBOODacrovinungh, see edited answer. Happy now? – Wildcard Apr 8 '16 at 4:36
0

That should be as simple as:

for i in A B C; do
    echo "$i"
done
  • Did I misunderstand the question? – pfnuesel Apr 8 '16 at 4:24
  • no but he wants command to be executed – cutzero Apr 8 '16 at 4:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.