<?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>0001813056</rptOwnerCik>
            <rptOwnerName>Pupkin Sergio A</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>false</isOfficer>
            <isTenPercentOwner>false</isTenPercentOwner>
            <isOther>false</isOther>
        </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>575</value>
                    <footnoteId id="F1"/>
                    <footnoteId id="F2"/>
                </transactionShares>
                <transactionPricePerShare>
                    <value>0</value>
                </transactionPricePerShare>
                <transactionAcquiredDisposedCode>
                    <value>A</value>
                </transactionAcquiredDisposedCode>
            </transactionAmounts>
            <postTransactionAmounts>
                <sharesOwnedFollowingTransaction>
                    <value>1777</value>
                </sharesOwnedFollowingTransaction>
            </postTransactionAmounts>
            <ownershipNature>
                <directOrIndirectOwnership>
                    <value>D</value>
                </directOrIndirectOwnership>
            </ownershipNature>
        </nonDerivativeTransaction>
    </nonDerivativeTable>

    <derivativeTable>
        <derivativeTransaction>
            <securityTitle>
                <value>Stock Appreciation Right</value>
                <footnoteId id="F2"/>
                <footnoteId id="F3"/>
            </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>987</value>
                </transactionShares>
                <transactionPricePerShare>
                    <value>0</value>
                </transactionPricePerShare>
                <transactionAcquiredDisposedCode>
                    <value>A</value>
                </transactionAcquiredDisposedCode>
            </transactionAmounts>
            <exerciseDate>
                <value>2025-12-16</value>
            </exerciseDate>
            <expirationDate>
                <value>2033-12-16</value>
            </expirationDate>
            <underlyingSecurity>
                <underlyingSecurityTitle>
                    <value>Common Stock ($0.10 par value)</value>
                </underlyingSecurityTitle>
                <underlyingSecurityShares>
                    <value>987</value>
                </underlyingSecurityShares>
            </underlyingSecurity>
            <postTransactionAmounts>
                <sharesOwnedFollowingTransaction>
                    <value>987</value>
                </sharesOwnedFollowingTransaction>
            </postTransactionAmounts>
            <ownershipNature>
                <directOrIndirectOwnership>
                    <value>D</value>
                </directOrIndirectOwnership>
            </ownershipNature>
        </derivativeTransaction>
    </derivativeTable>

    <footnotes>
        <footnote id="F1">Represents an award of unrestricted Common Stock under the UniFirst Corporation 2023 Stock Option and Incentive Plan.</footnote>
        <footnote id="F2">These are annual grants for fiscal 2026.</footnote>
        <footnote id="F3">This stock appreciation right is exercisable in full on the grant date (12/16/2025) and is required to be settled in stock at the time of exercise. The grant has an expiration date of the earlier of December 16, 2033 or the second anniversary of the date that the grantee ceases to be a member of the Board of Directors.</footnote>
    </footnotes>

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