Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have this kind of GCC multilib wrapper set up:

#file: gcc
#!/usr/bin/env bash
gcc -m32 "$@"

which essentially just wraps a 64-bit multilib gcc to act as a non-multilib 32-bit gcc. When I build something (like binutils for example), this spawns hundreds of bash processes, until even fork fails. How can I work around this?

share|improve this question
up vote 8 down vote accepted

It appears you named your script gcc, put it in the path, and then called it recursively. Either name your script something different or use an explicit path to the gcc executable you actually want to use.

share|improve this answer
Ah yes, that is it. Thanks! – rubenvb Oct 26 '11 at 16:54
super like! :-) – Nikhil Mulley Dec 5 '11 at 16:46

Kevin has already found the core issue, namely that you're calling your own script recursively.

A simple way to avoid that is to call the wrapped program with an absolute path. A fancier way is to examine $PATH manually and skip your own script (as identified with $0).

find_command () {
  script_dir={1%/*}; command_name=${1##*/}
  IFS=':'; set +f
  for d in $PATH; do
    if [ "$d" = "$script_dir" ]; then continue; fi
    if [ -x "$d/$command_name" ]; then real_command="$d/command_name" break; fi
  set -f; unset IFS
  [ -n "$real_command" ]
find_command "$0" || {
  echo 1>&2 "$0: cannot find underlying command in \$PATH=$PATH"
  exit 2
exec "$0" -m32 "$@"

Some additional tips:

  • Use exec if you don't need the shell anymore once you've launched the real command. Some shells know to do this as an optimization, but not all.
  • Use #!/bin/sh rather than #!/bin/bash unless you're using bash features. On many systems, sh is a leaner, faster shell than bash, with fewer features but wrapper scripts rarely need these advanced features.
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.