0
#!/bin/sh

i=1
for day in mon tue wed thu fri
do
    echo "weekday $((i++)) : $day"

        if [ $i -eq 3 ];then
        break;
        fi
done
I used debug mode to check how this script it will work
  • i=1
  • for day in mon tue wed thu fri sat sun
  • echo 'weekday 1 : mon' weekday 1 : mon
  • '[' 2 -eq 3 ']'
  • for day in mon tue wed thu fri sat sun
  • echo 'weekday 2 : tue' weekday 2 : tue
  • '[' 3 -eq 3 ']'
  • break
Here I am not understand variable (i) how it will work form staring to end. Could you please give me some explanation about this ?

1 Answer 1

1

(first you are mixing syntax from bash (e.g. $((...)), while using #!/bin/sh as the interpreter so it is difficult to say with certainty what will happen.) If you are getting numeric evaluation from $((...)), your shell (whichever that may be) is interpreting the $((...)) correctly

What is happening, and what you would expect in bash, etc. is that i is being incremented explicitly when you call $((i++)) inside the loop. When you begin the script, you set i=1 and it maintains that value until you begin to loop. When you start your loop, e.g.:

for day in mon tue wed thu fri
do

The loop variable is day, so day will take the values mon, tue, wed, thu and fri on each successive iteration through the loop. i however will increase by 1 on each iteration:

echo "weekday $((i++)) : $day"

What is happening is $((i++)) is the same as i=$((i+1)), (or i=$(expr $i + 1) if you are more familiar with that). The arithmetic syntax you are using can be used for tests itself or for arithmetic. If called without the leading '$', the mathematical expression between the ((..)) is evaluated and all variable values updated.

If called with the leading '$', then the same evaluation/update occurs, but the value is dereferenced and the value of the expression is made available at the point of dereference, no different that doing the same to a variable. (e.g. i=1; echo "i $i").

In your case you are both evaluating (with the post-increment operator) and dereferencing with the '$' in echo "weekday $((i++)) : $day". So you would expect it to output:

weekday 1 : mon
weekday 2 : tue

as i is updated during each loop until i=3, then you would expect the loop to be exited with:

    if [ $i -eq 3 ];then
        break;
    fi

Which is what occurs. Let me know if you have any additional questions.

Note: The $((...)) syntax is not part of the POSIX shell specification, so I would not expect your script to be portable using #!/bin/sh. In reality many distribution simply softlink /bin/sh -> /bin/bash, so I suspect that is what is happening in your case.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.