It's seems your environment $PATH variable does not include /usr/bin/ path or may be timeout binary exists in somewhere else.
So just check path of timeout command using :
command -v timeout
and use absolute path in your script
Ex.
#!/bin/bash
/usr/bin/timeout 2 sleep 5
Update 1#
As per your update in question, it is function created in your shell. you can use absolute path in your script as mentioned in above example.
Update 2#
timeout command added from coreutils version => 8.12.197-032bb, If GNU timeout is not available you can use expect (Mac OS X, BSD, ... do not usually have GNU tools and utilities by default).
################################################################################
# Executes command with a timeout
# Params:
# $1 timeout in seconds
# $2 command
# Returns 1 if timed out 0 otherwise
timeout() {
time=$1
# start the command in a subshell to avoid problem with pipes
# (spawn accepts one command)
command="/bin/sh -c \"$2\""
expect -c "set echo \"-noecho\"; set timeout $time; spawn -noecho $command; expect timeout { exit 1 } eof { exit 0 }"
if [ $? = 1 ] ; then
echo "Timeout after ${time} seconds"
fi
}
Example:
timeout 10 "ls ${HOME}"
Source
type timeoutin shell?/usr/bin/timeoutexists or nottimeoutyou get if you runtype -a timeout? In general you will need to export the function if you have it implemented somewhere to use it in a script, but like @RahulPatil mentioned timeout is usually just a c function unless you have some other implementations.timeout is a shell functionexport -f timeoutto your.bashrcor wherever you have it implemented. And then re-source that init file (e.g. withsource .bashrc) and try running the script again.