Skip to content

bash: warning: setlocale: LC_CTYPE: cannot change locale (en_US@fi.UTF-8): Bad file descriptor #1486

@fsc-eriker

Description

@fsc-eriker

Describe the bug

I installed bash-completions@v2 from Homebrew and added the invocation stanza to my .bashrc. After this, I started seeing scary error messages when starting up my shell.

bash$ bash --login 
bash: warning: setlocale: LC_CTYPE: cannot change locale (en_US@fi.UTF-8): Bad file descriptor
bash: warning: setlocale: LC_CTYPE: cannot change locale (en_US@fi.UTF-8): Bad file descriptor
bash: warning: setlocale: LC_COLLATE: cannot change locale (en_US@fi.UTF-8): No such file or directory
bash: warning: setlocale: LC_CTYPE: cannot change locale (en_US@fi.UTF-8): Bad file descriptor
bash: warning: setlocale: LC_CTYPE: cannot change locale (en_US@fi.UTF-8): Bad file descriptor
bash: warning: setlocale: LC_COLLATE: cannot change locale (en_US@fi.UTF-8): No such file or directory

Eventually I traced these errors to /opt/homebrew/Cellar/bash-completion@2/2.16.0/share/bash-completion/bash_completion which apparently manipulates these variables slightly carelessly.

To reproduce

  1. Set up your Mac for English language, but some location in Europe
  2. Observe that LANG ends up with a funky value like en_US@rg=fizzzz.UTF-8
  3. Further observe that this causes these errors from bash_completion

Expected behavior

Most ideally I guess it would be nice if Apple didn't assign a value that many tools cannot handle.

Somewhat less ideally, bash_completion should cope with these weird values.

Versions (please complete the following information)

  • Operating system name/distribution and version: MacOS Sequoia 15.7.1
  • bash version, echo "$BASH_VERSION": 5.3.3(1)-release from Homebrew
  • bash-completion version, (IFS=.; echo "${BASH_COMPLETION_VERSINFO[*]}"): 2.16.0

Additional context

I worked around this by adding normalization of LANG and LC_CTYPE to my .bashrc before the invocation of bash_completion.

# Work around apparent MacOS bug
case $LC_CTYPE in UTF-8) LC_CTYPE=en_US.UTF-8;; esac
case $LANG in
 "" | en_FI.UTF-8)
     LANG=en_US.UTF-8;;
 *@rg=*zzzz.*)
     LANG=${LANG/@rg=*zzzz./.};;
esac

Debug trace

I don't think a full debug trace is called for here, but here is a selected snippet from the output of bash --login -x:

+++ . /opt/homebrew/etc/profile.d/bash_completion.sh
++++ '[' 'x5.3.3(1)-release' '!=' x -a $'x\\h:\\W${AWS_PROFILE+ {$AWS_PROFILE\\}}$(\n    git symbolic-ref --short HEAD 2>/dev/null | sed \'/./s/.*/ [&]/\')\\$ ' '!=' x -a x = x ']'
++++ '[' 5 -gt 4 ']'
++++ '[' -r /Users/me/.config/bash_completion ']'
++++ shopt -q progcomp
++++ '[' -r /opt/homebrew/Cellar/bash-completion@2/2.16.0/share/bash-completion/bash_completion ']'
++++ . /opt/homebrew/Cellar/bash-completion@2/2.16.0/share/bash-completion/bash_completion
+++++ BASH_COMPLETION_VERSINFO=(2 16 0)
...
+++++ set +o noglob
+++++ shopt -s nullglob
+++++ shopt -u failglob dotglob
+++++ local GLOBIGNORE= GLOBSORT=name
+++++ local LC_COLLATE=C LC_CTYPE=en_US@rg=fizzzz.UTF-8 LC_ALL=
bash: warning: setlocale: LC_CTYPE: cannot change locale (en_US@rg=fizzzz.UTF-8): Bad file descriptor

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions