Bug 2060919

Summary: A ksh script that sources profile /etc/profile.d/which2.sh will lead to error export: -f: unknown option
Product: Red Hat Enterprise Linux 8 Reporter: Welterlen Benoit <bwelterl>
Component: whichAssignee: Than Ngo <than>
Status: CLOSED DUPLICATE QA Contact: CS System Management SST QE <rhel-cs-system-management-subsystem-qe>
Severity: medium Docs Contact:
Priority: unspecified    
Version: 8.5   
Target Milestone: rc   
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2022-03-21 14:18:07 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Welterlen Benoit 2022-03-04 14:33:24 UTC
Description of problem:
A ksh script that sources profile /etc/profile.d/which2.sh will lead to error export: -f: unknown option

Version-Release number of selected component (if applicable):
RHEL 8.5
which-2.21-16.el8.x86_64

How reproducible:
always

Steps to Reproduce:
1. create a a.sh ksh script that sources profile files:
2. cat a.sh
#!/bin/ksh
. /etc/profile
3. => ./a.sh[4]: .[20]: export: -f: unknown option


Actual results:
./a.sh[4]: .[20]: export: -f: unknown option

Expected results:
No error

Additional info:
From RHEL 8.4 to RHEL 8.5:

The file /etc/profile.d/which2.sh has been updated between version 2.21-12.el8 and 2.21-16.el8:

From:

# Initialization script for bash and sh
  
if [ "$0" = "ksh" ] || [ "$0" = "-ksh" ] ; then
  alias which='(alias; typeset -f) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot'
else
  alias which='(alias; declare -f) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot'
fi

to 

# shellcheck shell=sh
# Initialization script for bash, sh, mksh and ksh

which_declare="declare -f"
which_opt="-f"
which_shell="$(cat /proc/$$/comm)"
which_shell=$0

if [ "$which_shell" = "ksh" ] || [ "$which_shell" = "mksh" ] || [ "$which_shell" = "zsh" ] ; then
  which_declare="typeset -f"
  which_opt=""
fi

which ()
{
(alias; eval ${which_declare}) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot "$@"
}

export which_declare
export ${which_opt} which

---

When the file is sourced from a script, the cat /proc/$$/comm is the name of the script, not the shell (a.sh), thus conditions for ksh are not met, and which_opt is by default "-f"

- One ugly workaround is to set the option only if shell = bash:
---
if [ "$which_shell" = "bash"  ] || [ "$which_shell" = "sh" ] ; then
  which_declare="declare -f"
  which_opt="-f"
fi
---

And let the default to nothing.

Thanks

Comment 1 Welterlen Benoit 2022-03-21 14:18:07 UTC

*** This bug has been marked as a duplicate of bug 2025709 ***