• Check it out - we have a chatbot! Go ask TuxBot a question in the Ask Tuxbot section!

bash scripting question

let_s_begin

New Member
Joined
Jan 16, 2020
Messages
4
Reaction score
0
Credits
14
i was picking apart some bash scripts online to help me practice and learn more about bash. i encountered one script that had this ((count++)) towards the end of the script. from what i can tell this says to add to the variable. i was hoping for a better explanation about what it does, why its there, and hopefully some examples of its use in other scripts.

Here's the script

#!/bin/bash
valid=true
count=1
while [ $valid ]
do
echo $count
if [ $count -eq 5 ];
then
break
fi
((count++)) #this is what i was curios about
done
 


dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
1,677
Reaction score
1,296
Credits
11,063
hint: count = 1

you have a loop after this, what does it do?
while (waiting for something to be true)
do (something)
echo the value of $count
if the value of count equals 5, then break (stop running this script)

question. If the value of count starts off = 1
and we have a loop that is waiting for count to equal 5.

It probably does something to the value of count.
what would you guess that count++ does?
 

captain-sensible

Well-Known Member
Joined
Jun 14, 2019
Messages
2,910
Reaction score
1,970
Credits
18,114
And -eq means equal to so loop goes
2
3
4
5
At 5 breaks out of loop
 
MALIBAL Linux Laptops

Linux Laptops Custom Built for You
MALIBAL is an innovative computer manufacturer that produces high-performance, custom laptops for Linux.

For more info, visit: https://www.malibal.com


Top