Skip to content

Instantly share code, notes, and snippets.

@Lohann
Created December 6, 2025 19:16
Show Gist options
  • Select an option

  • Save Lohann/39816e43b5dae0091fa903d636eeaa20 to your computer and use it in GitHub Desktop.

Select an option

Save Lohann/39816e43b5dae0091fa903d636eeaa20 to your computer and use it in GitHub Desktop.
Shell script that prints recursively what is the current shell emulator.
#!/bin/sh
# Prints recursively what is the current shell emulator.
pid=$$
printf 'pid="%s"\n' "${pid}"
test "${pid}" = '1' && exit 1
die(){
echo "${1}"
exit 1
}
[ ! -x "$(command -v ps)" ] && die 'ps not found'
[ ! -x "$(command -v tail)" ] && die 'tail not found'
[ ! -x "$(command -v awk)" ] && die 'awk not found'
[ ! -x "$(command -v grep)" ] && die 'grep not found'
walk_ppid(){
pid="$(ps -h -o ppid -p "${pid}" 2>/dev/null | tail -n -1 | awk '{print $NF}')"
printf 'pid="%s"\n' "${pid}"
cmd="$(ps -h -o comm -p "${pid}" 2>/dev/null | tail -n -1)"
printf 'command="%s"\n\n' "${cmd}"
}
re='^[0-9]+$'
while [ "${pid}" -gt 1 ]
do
if ! echo "${pid}" | grep -Eq "${re}"; then
printf 'not an integer "%s"' "${pid}"
exit 1
fi
walk_ppid "${pid}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment