Skip to main content
added 4 characters in body
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60

In bash:

$ s() { printf 'Called s %s\n' "$*" >&2; }

$ cat $HOME/bin/command_not_found_handle
#!/usr/bin/env bash

command_not_found_handle() {
    if [[ "$*" =~ ^(s)([0-9]{3})$ ]]; then
        "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}"
    else
        printf 'Command not found: %s\n' "$*" >&2
        return 127
    fi
}

$ . $HOME/bin/command_not_found_handle

$ s123
Called s 123

$ s500
Called s 500

$ echo $?
0

$ s5001
Command not found: s5001

$ echo $?
127

FYI 127 is the standard "command not found" exit status. See https://www.gnu.org/software/bash/manual/bash.html#Command-Search-and-Execution for more information.

I'm only using BASH_REMATCH[] as above to call s because I know it's safe to do so given that $* contains the command as a single letter and its args just 3 digits, for anything more complicated see https://mywiki.wooledge.org/BashFAQ/050.

There is no solution that will work in every shell, see How to define a function that handles `command not found`? to find information on approaches to the above for some other shells.

In bash:

$ s() { printf 'Called s %s\n' "$*" >&2; }

$ cat $HOME/bin/command_not_found_handle
#!/usr/bin/bash

command_not_found_handle() {
    if [[ "$*" =~ ^(s)([0-9]{3})$ ]]; then
        "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}"
    else
        printf 'Command not found: %s\n' "$*" >&2
        return 127
    fi
}

$ . $HOME/bin/command_not_found_handle

$ s123
Called s 123

$ s500
Called s 500

$ echo $?
0

$ s5001
Command not found: s5001

$ echo $?
127

FYI 127 is the standard "command not found" exit status. See https://www.gnu.org/software/bash/manual/bash.html#Command-Search-and-Execution for more information.

I'm only using BASH_REMATCH[] as above to call s because I know it's safe to do so given that $* contains the command as a single letter and its args just 3 digits, for anything more complicated see https://mywiki.wooledge.org/BashFAQ/050.

There is no solution that will work in every shell, see How to define a function that handles `command not found`? to find information on approaches to the above for some other shells.

In bash:

$ s() { printf 'Called s %s\n' "$*" >&2; }

$ cat $HOME/bin/command_not_found_handle
#!/usr/bin/env bash

command_not_found_handle() {
    if [[ "$*" =~ ^(s)([0-9]{3})$ ]]; then
        "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}"
    else
        printf 'Command not found: %s\n' "$*" >&2
        return 127
    fi
}

$ . $HOME/bin/command_not_found_handle

$ s123
Called s 123

$ s500
Called s 500

$ echo $?
0

$ s5001
Command not found: s5001

$ echo $?
127

FYI 127 is the standard "command not found" exit status. See https://www.gnu.org/software/bash/manual/bash.html#Command-Search-and-Execution for more information.

I'm only using BASH_REMATCH[] as above to call s because I know it's safe to do so given that $* contains the command as a single letter and its args just 3 digits, for anything more complicated see https://mywiki.wooledge.org/BashFAQ/050.

There is no solution that will work in every shell, see How to define a function that handles `command not found`? to find information on approaches to the above for some other shells.

added 5 characters in body
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60

There is no solution that will work in any shell. In bash:

$ s() { printf 'Called s %s\n' "$*" >&2; }

$ cat $HOME/bin/command_not_found_handle
#!/usr/bin/bash

command_not_found_handle() {
    if [[ "$*" =~ ^(s)([0-9]{3})$ ]]; then
        "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}"
    else
        printf 'Command not found: %s\n' "$*" >&2
        return 127
    fi
}

$ . $HOME/bin/command_not_found_handle

$ s123
Called s 123

$ s500
Called s 500

$ echo $?
0

$ s5001
Command not found: s5001

$ echo $?
127

FYI 127 is the standard "command not found" exit status. See https://www.gnu.org/software/bash/manual/bash.html#Command-Search-and-Execution for more information.

I'm only using BASH_REMATCH[] as above to call s because I know it's safe to do so given that $* contains the command as a single letter and its args just 3 digits, for anything more complicated see https://mywiki.wooledge.org/BashFAQ/050.

There is no solution that will work in every shell, see How to define a function that handles `command not found`? to find information on approaches to the above for some other shells.

There is no solution that will work in any shell. In bash:

$ s() { printf 'Called s %s\n' "$*" >&2; }

$ cat $HOME/bin/command_not_found_handle
#!/usr/bin/bash

command_not_found_handle() {
    if [[ "$*" =~ ^(s)([0-9]{3})$ ]]; then
        "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}"
    else
        printf 'Command not found: %s\n' "$*" >&2
        return 127
    fi
}

$ . $HOME/bin/command_not_found_handle

$ s123
Called s 123

$ s500
Called s 500

$ echo $?
0

$ s5001
Command not found: s5001

$ echo $?
127

FYI 127 is the standard "command not found" exit status. See https://www.gnu.org/software/bash/manual/bash.html#Command-Search-and-Execution for more information.

I'm only using BASH_REMATCH[] as above to call s because I know it's safe to do so given that $* contains the command as a single letter and its args just 3 digits, for anything more complicated see https://mywiki.wooledge.org/BashFAQ/050.

In bash:

$ s() { printf 'Called s %s\n' "$*" >&2; }

$ cat $HOME/bin/command_not_found_handle
#!/usr/bin/bash

command_not_found_handle() {
    if [[ "$*" =~ ^(s)([0-9]{3})$ ]]; then
        "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}"
    else
        printf 'Command not found: %s\n' "$*" >&2
        return 127
    fi
}

$ . $HOME/bin/command_not_found_handle

$ s123
Called s 123

$ s500
Called s 500

$ echo $?
0

$ s5001
Command not found: s5001

$ echo $?
127

FYI 127 is the standard "command not found" exit status. See https://www.gnu.org/software/bash/manual/bash.html#Command-Search-and-Execution for more information.

I'm only using BASH_REMATCH[] as above to call s because I know it's safe to do so given that $* contains the command as a single letter and its args just 3 digits, for anything more complicated see https://mywiki.wooledge.org/BashFAQ/050.

There is no solution that will work in every shell, see How to define a function that handles `command not found`? to find information on approaches to the above for some other shells.

added 2 characters in body
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60

There is no solution that will work in any shell. In bash:

$ s() { printf 'Called s %s\n' "$*" >&2; }

$ cat $HOME/bin/command_not_found_handle
#!/usr/bin/bash

command_not_found_handle() {
    if [[ "$*" =~ ^(s)([0-9]{3})$ ]]; then
        "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}"
    else
        printf 'Command not found: %s\n' "$*" >&2
        return 127
    fi
}

$ . $HOME/bin/command_not_found_handle

$ s123
Called s 123

$ s500
Called s 500

$ echo $?
0

$ s5001
Command not found: s5001

$ echo $?
127

FYI 127 is the standard "command not found" exit status. See https://www.gnu.org/software/bash/manual/bash.html#Command-Search-and-Execution for more information.

I'm only using BASH_REMATCH[] as above to call s because I know it's safe to do so given that $* contains the command isas a single charletter and its args just 3 digits, for anything more complicated see https://mywiki.wooledge.org/BashFAQ/050.

There is no solution that will work in any shell. In bash:

$ s() { printf 'Called s %s\n' "$*" >&2; }

$ cat $HOME/bin/command_not_found_handle
#!/usr/bin/bash

command_not_found_handle() {
    if [[ "$*" =~ ^(s)([0-9]{3})$ ]]; then
        "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}"
    else
        printf 'Command not found: %s\n' "$*" >&2
        return 127
    fi
}

$ . $HOME/bin/command_not_found_handle

$ s123
Called s 123

$ s500
Called s 500

$ echo $?
0

$ s5001
Command not found: s5001

$ echo $?
127

FYI 127 is the standard "command not found" exit status. See https://www.gnu.org/software/bash/manual/bash.html#Command-Search-and-Execution for more information.

I'm only using BASH_REMATCH[] as above to call s because I know it's safe to do so given the command is a single char and args just 3 digits, for anything more complicated see https://mywiki.wooledge.org/BashFAQ/050.

There is no solution that will work in any shell. In bash:

$ s() { printf 'Called s %s\n' "$*" >&2; }

$ cat $HOME/bin/command_not_found_handle
#!/usr/bin/bash

command_not_found_handle() {
    if [[ "$*" =~ ^(s)([0-9]{3})$ ]]; then
        "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}"
    else
        printf 'Command not found: %s\n' "$*" >&2
        return 127
    fi
}

$ . $HOME/bin/command_not_found_handle

$ s123
Called s 123

$ s500
Called s 500

$ echo $?
0

$ s5001
Command not found: s5001

$ echo $?
127

FYI 127 is the standard "command not found" exit status. See https://www.gnu.org/software/bash/manual/bash.html#Command-Search-and-Execution for more information.

I'm only using BASH_REMATCH[] as above to call s because I know it's safe to do so given that $* contains the command as a single letter and its args just 3 digits, for anything more complicated see https://mywiki.wooledge.org/BashFAQ/050.

added 437 characters in body
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60
Loading
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60
Loading