sharkdp/fd · error · anyhow::Error

Unable to get shell from environment

Error message

Unable to get shell from environment

What it means

Thrown by Opts::gen_completions in src/cli.rs:781 when the user runs 'fd --gen-completions' (no shell name) and clap_complete::Shell::from_env() returns None. Shell::from_env reads the $SHELL environment variable and tries to map it to a known Shell variant; if $SHELL is unset or holds an unrecognized value, fd cannot guess which completion script format to emit.

Source

Thrown at src/cli.rs:781

    }

    pub fn strip_cwd_prefix<P: FnOnce() -> bool>(&self, auto_pred: P) -> bool {
        use self::StripCwdWhen::*;
        self.no_search_paths()
            && match self.strip_cwd_prefix.map_or(Auto, |o| o.unwrap_or(Always)) {
                Auto => auto_pred(),
                Always => true,
                Never => false,
            }
    }

    #[cfg(feature = "completions")]
    pub fn gen_completions(&self) -> anyhow::Result<Option<Shell>> {
        self.gen_completions
            .map(|maybe_shell| match maybe_shell {
                Some(sh) => Ok(sh),
                None => {
                    Shell::from_env().ok_or_else(|| anyhow!("Unable to get shell from environment"))
                }
            })
            .transpose()
    }
}

/// Get the default number of threads to use, if not explicitly specified.
fn default_num_threads() -> NonZeroUsize {
    // If we can't get the amount of parallelism for some reason, then
    // default to a single thread, because that is safe.
    let fallback = NonZeroUsize::MIN;
    // To limit startup overhead on massively parallel machines, don't use more
    // than 64 threads.
    let limit = NonZeroUsize::new(64).unwrap();

    std::thread::available_parallelism()
        .unwrap_or(fallback)
        .min(limit)

View on GitHub (pinned to 41532d114e)

Solutions

  1. Pass the shell explicitly: 'fd --gen-completions bash' (or zsh/fish/powershell/elvish).
  2. Export a recognized $SHELL: 'export SHELL=/bin/bash' then re-run 'fd --gen-completions'.
  3. If in CI/containers, set 'ENV SHELL=/bin/sh' in the Dockerfile or pipeline env block before invoking fd.

Example fix

// before
fd --gen-completions   # fails when $SHELL unset

// after
fd --gen-completions bash > /etc/bash_completion.d/fd
Defensive patterns

Strategy: validation

Validate before calling

# before invoking fd --gen-completions, ensure a shell is resolvable
if [ -z "$SHELL" ]; then
  echo "SHELL is unset; pass an explicit shell" >&2
  exit 1
fi
fd --gen-completions "$SHELL_BASE"   # or: fd --gen-completions bash

Prevention

When it happens

Trigger: Running 'fd --gen-completions' (double-flag form, Option<Option<Shell>> resolves to Some(None)) in an environment where $SHELL is unset, empty, or contains a binary name not recognized by the Shell enum (e.g. a custom shell wrapper like '/usr/bin/my-fish-fork').

Common situations: Minimal Docker/CI containers that strip env vars; running fd under a non-login shell that never set $SHELL; running fd from a systemd unit, cron job, or IDE task runner where the environment is sparse.

Related errors


AI-assisted analysis of sharkdp/fd@41532d114e (2026-08-06). Data as JSON: /data/errors/558d5501b4159ba9.json. Report an issue: GitHub.