<?xml version="1.0"?>
<ownershipDocument>

    <schemaVersion>X0508</schemaVersion>

    <documentType>4</documentType>

    <periodOfReport>2025-12-16</periodOfReport>

    <issuer>
        <issuerCik>0000717954</issuerCik>
        <issuerName>UNIFIRST CORP</issuerName>
        <issuerTradingSymbol>UNF</issuerTradingSymbol>
    </issuer>

    <reportingOwner>
        <reportingOwnerId>
            <rptOwnerCik>0001453910</rptOwnerCik>
            <rptOwnerName>Sintros Steven S</rptOwnerName>
        </reportingOwnerId>
        <reportingOwnerAddress>
            <rptOwnerStreet1>C/O UNIFIRST CORP</rptOwnerStreet1>
            <rptOwnerStreet2>68 JONSPIN ROAD</rptOwnerStreet2>
            <rptOwnerCity>WILMINGTON</rptOwnerCity>
            <rptOwnerState>MA</rptOwnerState>
            <rptOwnerZipCode>01887</rptOwnerZipCode>
            <rptOwnerStateDescription></rptOwnerStateDescription>
        </reportingOwnerAddress>
        <reportingOwnerRelationship>
            <isDirector>true</isDirector>
            <isOfficer>true</isOfficer>
            <isTenPercentOwner>false</isTenPercentOwner>
            <isOther>false</isOther>
            <officerTitle>President and CEO</officerTitle>
        </reportingOwnerRelationship>
    </reportingOwner>

    <aff10b5One>false</aff10b5One>

    <nonDerivativeTable>
        <nonDerivativeTransaction>
            <securityTitle>
                <value>Common Stock</value>
            </securityTitle>
            <transactionDate>
                <value>2025-12-16</value>
            </transactionDate>
            <transactionCoding>
                <transactionFormType>4</transactionFormType>
                <transactionCode>A</transactionCode>
                <equitySwapInvolved>false</equitySwapInvolved>
            </transactionCoding>
            <transactionAmounts>
                <transactionShares>
                    <value>5167</value>
                    <footnoteId id="F1"/>
                    <footnoteId id="F2"/>
                </transactionShares>
                <transactionPricePerShare>
                    <value>0</value>
                </transactionPricePerShare>
                <transactionAcquiredDisposedCode>
                    <value>A</value>
                </transactionAcquiredDisposedCode>
            </transactionAmounts>
            <postTransactionAmounts>
                <sharesOwnedFollowingTransaction>
                    <value>33982</value>
                    <footnoteId id="F3"/>
                </sharesOwnedFollowingTransaction>
            </postTransactionAmounts>
            <ownershipNature>
                <directOrIndirectOwnership>
                    <value>D</value>
                </directOrIndirectOwnership>
            </ownershipNature>
        </nonDerivativeTransaction>
    </nonDerivativeTable>

    <derivativeTable>
        <derivativeTransaction>
            <securityTitle>
                <value>Stock Appreciation Right</value>
                <footnoteId id="F2"/>
                <footnoteId id="F4"/>
            </securityTitle>
            <conversionOrExercisePrice>
                <value>174.2</value>
            </conversionOrExercisePrice>
            <transactionDate>
                <value>2025-12-16</value>
            </transactionDate>
            <transactionCoding>
                <transactionFormType>4</transactionFormType>
                <transactionCode>A</transactionCode>
                <equitySwapInvolved>false</equitySwapInvolved>
            </transactionCoding>
            <transactionAmounts>
                <transactionShares>
                    <value>7410</value>
                </transactionShares>
                <transactionPricePerShare>
                    <value>0</value>
                </transactionPricePerShare>
                <transactionAcquiredDisposedCode>
                    <value>A</value>
                </transactionAcquiredDisposedCode>
            </transactionAmounts>
            <exerciseDate>
                <footnoteId id="F4"/>
            </exerciseDate>
            <expirationDate>
                <value>2035-12-16</value>
            </expirationDate>
            <underlyingSecurity>
                <underlyingSecurityTitle>
                    <value>Common Stock ($0.10 par value)</value>
                </underlyingSecurityTitle>
                <underlyingSecurityShares>
                    <value>7410</value>
                </underlyingSecurityShares>
            </underlyingSecurity>
            <postTransactionAmounts>
                <sharesOwnedFollowingTransaction>
                    <value>7410</value>
                </sharesOwnedFollowingTransaction>
            </postTransactionAmounts>
            <ownershipNature>
                <directOrIndirectOwnership>
                    <value>D</value>
                </directOrIndirectOwnership>
            </ownershipNature>
        </derivativeTransaction>
    </derivativeTable>

    <footnotes>
        <footnote id="F1">Represents restricted stock units granted under the UniFirst Corporation 2023 Stock Option and Incentive Plan (the &quot;2023 Plan&quot;). Such restricted stock units vest in three equal annual installments on October 31, 2026, October 31, 2027 and October 31, 2028.</footnote>
        <footnote id="F2">These are annual grants for fiscal 2026.</footnote>
        <footnote id="F3">Consists of (i) 18,743 shares of Common Stock owned by the reporting person, (ii) 747 restricted stock units that vest in one remaining annual installment on October 31, 2026, (iii) 1,576 restricted stock units that vest in two remaining equal annual installments on October 31, 2026 and October 31, 2027, (iv) 2,738 restricted stock units that vest in three equal annual installments on October 31, 2026, October 31, 2027 and October 31, 2028, (v) 3,113 restricted stock units that vest in two equal annual installments on October 31, 2026 and October 31, 2027, (vi) 1,898 restricted stock units that vest in two equal annual installments on October 31, 2026 and October 31, 2027 and (vii) 5,167 restricted stock units that vest in three equal annual installments on October 31, 2026, October 31, 2027 and October 31, 2028.</footnote>
        <footnote id="F4">This stock appreciation right, which was granted under the 2023 Plan, becomes vested and exercisable in three equal annual installments on October 31, 2026, October 31, 2027 and October 31, 2028. This stock appreciation right is required to be settled in stock at the time of exercise.</footnote>
    </footnotes>

    <ownerSignature>
        <signatureName>/s/ John Dowd, Attorney-in-Fact</signatureName>
        <signatureDate>2025-12-18</signatureDate>
    </ownerSignature>
</ownershipDocument>
