0

I'm trying to create a bash alias that would act as a prefix, and could be parsed without needing to use a space. Basically, I have a set of commands that all start with the same first few characters, and I would like to prefix them with another command before running it without having to type that prefix out every time.

In other terms, I want to be able to create an alias that would work like this: alias pref="sudo commandtoprefix" would make it so that running prefcommand would run sudo commandtoprefixcommand. Note the lack of space, it is important.

I think I was able to do it at some point by simply doing as described, but for some reason it now doesn't work and returns a command not found error.

I've stumbled upon this answer, but it is describing a function and therefore requires the use of a space, which doesn't exactly fit my needs.

Is there a way I could do this with aliases or functions that wouldn't require using a space?

1
  • 1
    I don't think this is possible with a simple alias or function, but with bash, I think you could make a readline shortcut that replaces a string likeprf with sudo commandtoprefix anywhere in the command line. But the replacement cannot be the originally string. So pref for sudo commandtoprefix is not possible, but you can use prf, for example: bind '"prf": "sudo commandtoprefix"'.
    – muru
    Mar 31, 2022 at 9:27

2 Answers 2

1

You can make a function which does something similar.

function pref { prefix$*; }

And subsequently call pref command arg arg, calling prefixcommand arg arg.

0

If you really insist on not using spaces, and also insist that the list of possible commands to prefix in this way not be determined in advance, the only way I can think of to accomplish this would be to use a custom "command not found" handler.

Bash calls the function command_not_found_handle() if you enter a non-existing command. You could exploit this to have the function check if the first four letters are pref, and if so, check if what follows is the name of an actual command, and if so, run it with sudo, and otherwise just report the usual error.

command_not_found_handle() {
    # read the "missing command" entered
    missingcmd="$1"
    # remove it from the argument list
    shift
    # check if starts with "pref"
    if [[ "${missingcmd:0:4}" == "pref" ]] ; then
        # read part after "pref"
        prefixedcmd="${missingcmd:4}"
        # check if it's a real command
        if which "$prefixedcmd" &> /dev/null ; then
            # run sudo with the cmd and the rest of the argument list
            sudo "$prefixedcmd" "$@"
            return $?
        fi
    fi
    # default command not found behavior
    echo "bash: $missingcmd: not found" >&2
    return 127
}

Put that in your .bashrc.

EDIT: I realize I may have misunderstood your question; it may be that there's a common prefix to the commands you want to do this with already, and you want to leave that out when using the special sudo-adding prefix. That should be possible too if you just change my line:

prefixedcmd="${missingcmd:4}"

To

prefixedcmd="commonprefix${missingcmd:4}"

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .