score:1
Based on Barmar's suggestion, I was able to implement a workable solution. The following function can be used to rename an arbitrary bash function to another name.
renameFunction () {
local oldName="$1"; shift
local newName="$1"
local definition="$(declare -f "$oldName")"
if [[ $? -gt 0 ]]; then
echo >&2 "renameFunction: $oldName is not a function"
return
fi
if declare -f "$newName" >/dev/null 2>/dev/null; then
echo >&2 "renameFunction: $newName is already defined"
return
fi
eval "$(echo "${definition/"$oldName"/"$newName"}")"
# Does not work for recursive functions (like "//" would), but also
# doesn't break if $oldName is a substring of something else
unset "$oldName"
}
Notes
The last line
unset "$oldName"
is optional — and without it, this becomes a "copy function" utility.
The pattern substitution would work for a recursive function if it were changed to the following (note the
//
):eval "$(echo "${definition//"$oldName"/"$newName"}")"
However, this fails if the function name is a substring of something else within the definition. Since recursion is relatively rare in shell scripts, I took the less brittle approach.
The quoting is correct, despite being too complex for the SO syntax highlighter. (The quoting is also unnecessary, unless you like to play with
$IFS
.)
For completeness' sake, here's how I'm using this function:
# The DEFAULT_CMD is the command to run if the command line could
# not be understood. Set the DEFAULT_CMD to git, once; the user can
# change it at any time
DEFAULT_CMD=git
# Save the old command_not_found_handle for reuse
renameFunction command_not_found_handle __PREVIOUS_COMMAND_NOT_FOUND_HANDLE
command_not_found_handle () {
eval '"$DEFAULT_CMD" $DEFAULT_CMD_PREFIX_ARGS "$@" $DEFAULT_CMD_POSTFIX_ARGS'
if [ $? -gt 0 ]; then
__PREVIOUS_COMMAND_NOT_FOUND_HANDLE "$@"
fi
}
export DEFAULT_CMD
command_not_found_handle
is called by bash
whenever it cannot find
program or command that the user specified. It receives as its arguments
the entire command-line.
This function tries to execute the command-line as a "sub command" of
the given DEFAULT_CMD
. If it does not succeed, it tries the old
command_not_found_handle
Source: stackoverflow.com
Related Query
- Save the old value of a function bash, so that it can be called later
- In a bash script that starts with set -e, can I set the exit code to a different value than the first failed command's?
- how to write a Bash function that confirms the value of an existing variable with a user
- How can I adjust my bash function such that I can omit the double-quotes?
- BASH - how can i search a value from whole directory and find some matched value, then on the fly replace that line by saving it
- Can a bash function or script be written that starts off the next command
- BASH function that handles the exiting of the functions it's called in
- How can I change the environment of my bash function without affecting the environment it was called from?
- How can I do a function that outputs the not of another function in bash shell?
- How to write a Bash function that can generically test the output of executed commands?
- Can I find the line in a bash script a function was called from without a debugger/stacktrace?
- how to list the variables defined by a Bash function, including those pre-existing variables that are unchanged in value by the function
- How can I get iTerm to use the newer version of bash that brew shows? Change a user's shell on OSX
- Variable in Bash Script that keeps it value from the last time running
- in Bash you can set -x to enable debugging, is there any way to know if that has been set or not from within the script?
- How can a sourced bash snippet conditionally provide a function to the sourcing shell?
- Is there a bash command that can tell the size of a shell variable
- How can I debug a Bash function that returns a value, and how can I add newlines to a variable?
- How to stop a bash script when the python script that is called within bash encounters an error?
- Error handling inside a function so that we can exit the script
- How to use function return value in while test in the bash script
- psql return value / error killing the shell script that called it?
- Can I create a bash script that relies on the exit code from the previously executed command in my shell?
- Why can I run a Bash function with the 000 permissions?
- Bash Function is not getting called, unless I echo the return value
- bash save last user input value permanently in the script itself
- Use bash alias name in a function that was called using that alias
- How can I get my Cocoa command line tool to know the working path that it was called from in the terminal?
- bash - Directly returning the return value from calling a function
- how to make a generic Bash function that can accept information via positional and named arguments
More Query from same tag
- Use grep xargs sed to regenerate UUIDs in a file more effeciently
- Bash array created from file won't create a string index
- Linux Bash - load declared variable from textfile
- how can I find and replace the string with quote?
- How to access literal wildcard argument before it's expanded to matching files?
- The best way to check that we have require version software in bash script
- Ways to provide list of parameters to options in shell scripts?
- BASH: Substituting a variable inside a variable during echo
- Simple shell script to copy files in S3 using s3cmd on Mac
- if else statement in bash
- shell script, need help to check if file exists
- No output on bash program
- Bash selecting files from directory listing stored in a string
- Adding sudo permissions to sudoers for user via shell script
- cannot update PHP on mac osx 10.9
- How can Linux program, e.g. bash or python script, know how it was started: from command line or interactive GUI?
- Is it possible to clean up an HTML file with grep to extract certain strings?
- Bash :what is the syntax to create one spaces with sed
- Convert special character (√) into normal string using bash
- bash script porting issue related to script program
- "$HOME/.rvm/scripts/rvm" Not working in OSX Lion .bash_profile
- Splitting multiple input files into multiple outputs using split function in linux
- Adding line number and file name to 3 column files using awk
- Passing folder as flag in regex
- Use result from mongodb in shell script
- Sed to remove user specified block
- How to edit output of "tail"?
- Discovering remote Terminal for Terminal Escape Codes? (DECDHL in this case)
- Different positions of caret in the pattern of grep command?
- Running command in Linux after # symbol