I use Bash 5.1.8. Running man
shows the manual page but with the following errors
man ps
sh: bat: line 10: syntax error near unexpected token `('
sh: bat: line 10: ` *.?(ba)sh)'
sh: error importing function definition for `bat'
I think some shell (sh
) is finding Bashisms obnoxious. These errors vanish if I remove Bash-isms like the following from ~/.bashrc
:
function bat {
# lines snipped for brevity
case "$f" in
*.rs ) opt_syntax="--syntax=rust";;
*.?(ba)sh ) opt_syntax="--syntax=shellscript";;
*.?(m)m ) opt_syntax="--syntax=objc";;
esac
# lines snipped for brevity
}
export -f bat
I'm sure .bashrc
itself has no problems, as I see no errors or warnings when Bash starts. Debugging further I noticed .profile
sourcing .bashrc
# source Bash customizations
[ -n "${BASH_VERSION}" ] && [ -r "${HOME}/.bashrc" ] && . "${HOME}/.bashrc"
Here's how my ~/.bashrc
starts
# If not running interactively, don't do anything
[[ "$-" != *i* ]] && return
Questions:
- Why does
man
have to source.profile
just before starting? - Despite two checks why is the aforementioned code parsed by a non-Bash shell?
- Check in
.profile
to not source.bashrc
when it's not Bash - Check in
.bashrc
to stop further processing when non-interactive
- Check in
From @muru's comments I realize that I should NOT have Bashisms in a function that's exported since there's a risk of it being imported by a non-Bash shell. A question that still remains: why man
calls sh
?.
export -f bat
. Aah... so it can't have Bash-isms after all?!sh
will try to import function definitions, but some behaviour will change. See gnu.org/software/bash/manual/bash.html#Bash-POSIX-Modeexport
does solve the issue. Will read about Bash's POSIX mode; thanks @muru! However, I still don't see why.profile
is sourced byman
..profile
being sourced. If it was, the error would have mentioned the file being sourced. I think it's only the exported functions being imported bysh
. Not sure whyman
is runningsh
, though. Maybe for paging?