BashSharp 1.0.1

dotnet add package BashSharp --version 1.0.1
                    
NuGet\Install-Package BashSharp -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BashSharp" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BashSharp" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="BashSharp" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add BashSharp --version 1.0.1
                    
#r "nuget: BashSharp, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=BashSharp&version=1.0.1
                    
Install BashSharp as a Cake Addin
#tool nuget:?package=BashSharp&version=1.0.1
                    
Install BashSharp as a Cake Tool

BashSharp

A cross-platform C# library for executing bash commands with strongly-typed results. Works on Windows (via WSL), Linux, and macOS.

NuGet Build Status

Features

  • Execute bash commands and get strongly-typed results
  • Cross-platform support (Windows via WSL, Linux, and macOS)
  • Async/await support with cancellation and timeout
  • Error handling and exit code tracking
  • Strongly-typed command results via custom result models
  • Efficient streaming output handling for large outputs

Installation

Install via NuGet:

dotnet add package BashSharp

Prerequisites

  • .NET 8.0 or later
  • On Windows: Windows Subsystem for Linux (WSL) with Debian
  • On Linux/macOS: bash shell

Usage

Basic Command Execution

// Simple execution - returns success/failure
bool success = await BashCommandService.ExecuteCommand("echo 'Hello World'");

// Get exit code
int exitCode = await BashCommandService.ExecuteCommandWithCode("ls -la");

// Get strongly-typed results
public class LsResult : ICommandResult
{
    public List<string> Files { get; private set; } = new();
    public int ExitCode { get; private set; }

    public void SetExitCode(int exitCode) => ExitCode = exitCode;
    
    public void ParseResult(string output)
    {
        Files = output.Split('\n')
            .Where(line => !string.IsNullOrWhiteSpace(line))
            .ToList();
    }

    public void ParseError(string error) { }
}

var result = await BashCommandService.ExecuteCommandWithResults<LsResult>("ls");
foreach (var file in result.Files)
{
    Console.WriteLine(file);
}

Timeout and Cancellation

// With timeout
await BashCommandService.ExecuteCommand("long-running-command", timeoutMs: 5000);

// With cancellation
using var cts = new CancellationTokenSource();
var task = BashCommandService.ExecuteCommand("long-running-command", cancellationToken: cts.Token);
// Cancel after 1 second
await Task.Delay(1000);
cts.Cancel();

Error Handling

The library throws exceptions when commands fail or return non-zero exit codes. Use try-catch blocks to handle errors:

try 
{
    await BashCommandService.ExecuteCommand("invalid-command");
}
catch (Exception ex)
{
    Console.WriteLine($"Command failed: {ex.Message}");
}

Platform Support

  • Windows: Requires Windows Subsystem for Linux (WSL) with Debian
  • Linux: Requires bash shell
  • macOS: Requires bash shell

Windows Setup

  1. Enable WSL:
wsl --install -d Debian
  1. Set WSL 2 as default:
wsl --set-default-version 2

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

This project is licensed under the terms of the LICENSE file included in the repository.

Acknowledgments

  • Thanks to all contributors
  • Built with .NET 8.0
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 105 1/20/2025
1.0.0 90 1/16/2025