Bash 4.0 introduced new functionality for handling commands that are not found -- it will call a function named command_not_found_handle
, if that function exists. You could use that behavior to intercept calls to "programs" named dfk-NNN or DFK-NNN and re-run them as you like. Since we're depending on 4.0 behavior already, I've used the upper-casing and lower-casing parameter expansion features as well.
command_not_found_handle() {
case "${1,,}" in
(dfk-?|dfk-??|dfk-???|dfk-????|dfk-?????)
echo dfkp "${1^^}" "${@:2}"
return 0
;;
(*)
printf 'bash: %s: command not found\n' "$1" >&2
return 127
;;
esac
}
If you enter a command that doesn't exist, bash calls this function with the original command-line. The case
statement lower-cases the first argument so that it's simpler to compare it against the dfk
patterns required.
If the command starts with dfk-
and ends with 1, 2, 3, 4, or 5 characters (tighten this to be digits as you see fit), then it -- currently echos, for debugging, but then -- calls the dfkp
program with the first parameter being the upper-cased version of the initial dfk entry, followed by the remainder of the command-line.
For commands that don't match dfk-NNNN
, it falls through and prints a simulated bash error message to stderr and returns 127. (This matches the default behavior, as if command_not_found_handle did not exist).
A sample session (with the echo debugging installed, as I don't have an actual dfkp program):
$ dfk-1234
dfkp DFK-1234
$ dfk-18875 cat file.txt
dfkp DFK-18875 cat file.txt
$ some-other-command
bash: some-other-command: command not found
$ dfk-1
dfkp DFK-1
$ dfk-12345
dfkp DFK-12345
dfk 18875
instead? That's same amount of typing.dfk 12345
? You could write a function calleddfk
which would then take the first argument as the target number and rundfpk DFK-12345
as requested. What's missing in that approach?dfk 12345
would mean I then have to backspace and delete the hyphen. Or type dfk then paste it. I want to avoid all that. I can easily write a function as you describe. I want to just paste and hit enter.