'Unable to locate executable file: 'bash' error for a simple script on Azure Devops Pipeline

I get the following error if I run my azure devops pipeline:

##[error]Unable to locate executable file: 'bash'. Please verify either the file path exists or the 
file can be found within a directory specified by the PATH environment variable. Also check the file 
mode to verify the file is executable.

I tried it with my self hosted agent and with microsoft hosted agent, both of them have the same error.

Here is my pipeline:

trigger:
 - master

pool:
  vmImage: 'ubuntu-latest'

steps:
 - script: 
    echo Starting the build
    ./gradlew build
   displayName: 'Build


Solution 1:[1]

You need to set the workingDirectory for the script like documnted here: Docs

- script: # script path or inline
  workingDirectory: #
  displayName: #

For example if you have a repository with a structure like this:

RepoName
 - Folder1
   - scriptToRun
 - Folder2

The value for workingDirectory would be:

- script: ./scriptToRun
  workingDirectory: '$(Build.SourcesDirectory)/Folder1'
  displayName: 'Build'

Or you can probably skip the workingDirectory and give a full path to the script. Note also tht this example is for a single repository checkout. If you checkout multiple repos the path would be different.

Solution 2:[2]

I had the same problem, initially I had given my self-hosted agent, a PAT with access to the Agent Pools Read/Manage permission, as this is what it states in the docs, and then I started getting this error, even though bash was clearly in my PATH environment variable.

I then removed the agent, added it back, and this time gave it a PAT with Full Access.

I no longer receive the bash error.

Solution 3:[3]

I just had this issue and after renaming a variable that it was named path, it worked.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Repcak
Solution 2 Greg Quinn
Solution 3 Henrique Brisola