Skip to content

#!/usr/local/bin/powershell bang should not load profile #992

Closed

Description

@vors

Steps to reproduce

Put in your PS profile

Write-Host 'PROFILE'

Create a script ./foo.ps1 and chmod +x on it

#!/usr/local/bin/powershell
Write-Host 'foo'

From bash call ./foo.ps1

Expected behavior

Should not load profile

ash-3.2$ ./foo.ps1
foo

Reason: that's how bash handles it

Actual behavior

Load profile

ash-3.2$ ./foo.ps1
PROFILE
foo

Environment data

v0.4.0

Activity

  1. added
    Usability(LEGACY) Helps filter issues that might be higher priority because they directly affect usability
    on May 18, 2016
  2. andyleejordan commented on May 18, 2016

    @andyleejordan
    Member

    Ha! I don't see anyway this is going to work without a shim that executes with -noprofile.

  3. vors commented on May 19, 2016

    @vors
    CollaboratorAuthor

    We just need to figure out who called us, you know.

    Also, if you run the same way

    #!powershell
    
    Write-Host 'foo'
    bash-3.2$ /Users/vors/dev/foo.ps1
    Failed to load , error: dlopen(, 1): no suitable image found.  Did find:
        /usr/local/lib/: not a file
        /usr/lib/: not a file
    
    
  4. andyleejordan commented on May 19, 2016

    @andyleejordan
    Member

    I don't see how we would reliably determine who called us, and how to interpret it (I as a user might call PowerShell via env, and wouldn't want its behavior to change because it detected that env launched it instead of Bash, that's just a bad idea).

    I think really we just want to run with -noprofile whenever an in-place script is run (and -noexit was not used). Said another way, a profile should only be loaded when PowerShell is launched into an interactive shell.

    This would be much better behavior (but we'd need a way to specify -profile to reverse it).

  5. vors commented on May 19, 2016

    @vors
    CollaboratorAuthor

    Jason Shirk (@lzybkr) can you comment on the profile/noprofile, please ?

    Should this work at all #!powershell ?

  6. modified the milestones: Aug17, Future on Jun 8, 2016
  7. lzybkr commented on Jul 21, 2016

    @lzybkr
    Contributor

    I just noticed this in my .bashrc:

    # If not running interactively, don't do anything
    case $- in
        *i*) ;;
          *) return;;
    esac
    

    So I would say we should run the profile with #!powershell, but we should also find a clean way of detecting the process is interactive, e.g. if ($host.IsInteractive)

  8. vors commented on Jul 21, 2016

    @vors
    CollaboratorAuthor

    👍
    I was asking for $host.IsInteractive for ages.

  9. modified the milestones: , Aug17, Future on Jul 21, 2016
  10. 36 remaining items

  11. rkeithhill commented on Jun 5, 2018

    @rkeithhill
    Collaborator

    It would really be nice to have something for 6.1 that works on Windows as well. I've been trying to use git pre-commit hooks on my Windows dev box to ensure I have the right email configured. This works but it slows down all my commits because it processes my profile scripts when it does not need to:

    #!/usr/bin/env pwsh
    
    # Verify user's Git config has appropriate email address
    if ($env:GIT_AUTHOR_EMAIL -notmatch '@(non\.)?acme\.com$') {
        Write-Warning "Your Git email address '$env:GIT_AUTHOR_EMAIL' is not configured correctly."
        Write-Warning "It should end with '@acme.com' or '@non.acme.com'."
        Write-Warning "Use the command: 'git config --global user.email <[email protected]>' to set it correctly."
        exit 1
    }
    
    exit 0
    

    The only work around I've found is to put the PS script in a separate pre-commit.ps1 file and modify pre-commit to:

    #!/bin/sh
    pwsh.exe -NoProfile -File .git/hooks/pre-commit.ps1
    

    But I'd rather there be just a single file. At this point, I'd be satisfied if I could do this:

    #!/usr/bin/env pwsh-np
    ...
    
  12. mklement0 commented on Jun 5, 2018

    @mklement0
    Contributor

    Keith Hill (@rkeithhill):

    It's obviously just a stopgap, but you can roll your own pwsh-np by creating a script by that name with the content below and by placing it in a folder in your PATH (works on both Windows and Unix-like platforms, but on the latter you must also mark the script as executable (chmod a+x pwsh-np)):

    #!/bin/sh
    
    pwsh -noprofile "$@"

    Your hooks should then be able to use the shebang you mentioned (#!/usr/bin/env pwsh-np).

  13. nippyin commented on May 20, 2019

    @nippyin

    Able to Run .ps1 script on zsh manually, however if call same ps1 from ansible playbook gets error that Connect-AzAccount is not recognized. #!/usr/bin/env pwsh is what i use in .ps1
    Please help me to setup my environment variable.

  14. iSazonov commented on May 20, 2019

    @iSazonov
    Collaborator

    Sandeep Sharma (@nippyin) Please open new issue with step-by-step repo.

  15. rashil2000 commented on Sep 4, 2021

    @rashil2000

    I just noticed this in my .bashrc:

    # If not running interactively, don't do anything
    case $- in
        *i*) ;;
          *) return;;
    esac
    

    So I would say we should run the profile with #!powershell, but we should also find a clean way of detecting the process is interactive, e.g. if ($host.IsInteractive)

    Is $Host.IsInteractive planned? FWIW, [Environment]::UserInteractive always seems to return True, even in pwsh -c <command>.

    I just realized that setting PowerShell as default shell on Linux (using chsh) is a very bad idea. All my programs display a lag of 3-4 seconds when starting up. Many applications use "$SHELL" -c <command> to run external commands, and in each of them PowerShell seems to load the profile, causing the delay (since $SHELL is now /usr/bin/pwsh). This is particularly noticeable in Neovim and Fzf, as examples.

    Presence of $Host.IsInteractive would really help in aborting the sourcing of profile, as in the case of bash.

  16. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  17. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  18. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  19. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Committee-ReviewedPS-Committee has reviewed this and made a decisionIssue-Enhancementthe issue is more of a feature request than a bugOS-LinuxResolution-No ActivityIssue has had no activity for 6 months or moreUsability(LEGACY) Helps filter issues that might be higher priority because they directly affect usabilityWG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions