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,448
Reaction score
1,054
Credits
8,951
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,864
Reaction score
1,936
Credits
17,734
And -eq means equal to so loop goes
2
3
4
5
At 5 breaks out of loop
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Linux.org Hosting Donations
Consider making a donation

Members online


Top