Executing a script through "sudo" makes the script report "$0" as "/dev/fd/6" instead of expected script name

Solution Verified - Updated -

Issue

  • When executing a script through sudo, checking $0 or ${BASH_SOURCE[0]} environment variable reports /dev/fd/6 instead of expected script name, as shown in the example below

    # cat /tmp/test
    #!/bin/sh
    echo "This is $0"
    
    # sudo /tmp/test
    This is /dev/fd/6
    
  • The issue only occurs when sudo is configured to verify the checksum, e.g.

    %wheel ALL=NOPASSWD: sha512:...  /tmp/test
    

Environment

  • Red Hat Enterprise Linux 7 and later
    • sudo

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content