Sample Header Ad - 728x90

sh: which: line 1: syntax error: unexpected end of file; sh: error importing function definition for `which'

4 votes
2 answers
14580 views
We are running: Red Hat Enterprise Linux release 8.5 (Ootpa). We allow the server to update weekly using "yum -y update". We recently began receiving the following errors when executing shell commands: - sh: which: line 1: syntax error: unexpected end of file - sh: error importing function definition for `which' I am not aware of anything we changed that could cause this error -- this why I am pointing to the yum system update. Much of our code is written in php where we use passthru() to run shell commands. When we rarely write a shell script we generally use bash. I have the ability to prevent these errors using "unset which" prior to running a php script that uses passthru(). We run many shell commands from many scripts. Updating each of these scripts is not an effective solution -- time consuming and potentially introduces defects. It is interesting to note that the following code Does Not cause an error:
#!/bin/sh
echo hello
Is there a way to 'unset which' once at the system or user level so it would be executed each time we run the shell? Perhaps there is a shell profile that I can change or something similar. **Update 1** I found the following in /etc/profile.d/which2.sh
# shellcheck shell=sh
# Initialization script for bash, sh, mksh and ksh

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

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
The syntax looks correct here. I can tell that it exports 'which' -- but I'm not sure why. If I knew what this is doing, I could evaluate the risk of
which
. **Update 2** Created a small php script:
#!/usr/bin/php
This Does create the error.
Asked by Greg Sims (211 rep)
Dec 14, 2021, 08:49 PM
Last activity: Mar 28, 2023, 02:00 PM