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
- Loop through the files from multible directories as function in bash
- How could I import a certain function in another shell script while don't execute the entire code in it?
- Condensing a Bash script to parse a file
- Is it possible to define or limit a command line alias to only work in a particular directory?
- OpenSSL key length too short in Ruby, not in Bash
- In Bash, how to print to stdout when it’s already redirected?
- What characters should be escaped in Expect, and how?
- Assigning one variable the value of another in bash
- Awk, System() function, interacting with shell
- How to add a specific word next to the finding word in a text file using Shell Script?
- Newline Before & After Prompt (Editing An Edited .bash_profile)
- Bash confirmation won't wait for user input
- Problem setting python environment from a script
- how to get basename in -exec of find?
- "sh runMyCode.sh" not loops in format "for n in {10..99}"
- Problem with temporary unnamed pipe in bash script
- How to print the body content of a html page using sed
- Rename files if not the current file
- bash find command for counting files does not work
- How to execute shell command get the output and pwd after the command in Python
- Why is execvp() executing twice using fork()?
- How to find the latest folder in a directory and then make a full path accordingly?
- Calling an executable within a nested for loop in linux
- Remove tracked file from Git when file has $$ in name
- How to dynamically compute value of a variable and use it in a command?
- Chaining Bash Statements
- Can a Bash function in pipe knows anything about the next function after it?
- Write to two columns
- Extract common files from two projects into a third project, preserving directory structure
- Shell: Checking if argument exists and matches expression