0

BASH SHELL; DEBIAN STRETCH

As seen here, I've created a "seems to work OK, most of the time" alias function for the su command (actually, I'm not able to find any problems with it...yet). I've inserted it into my /etc/bash.bashrc file.

function su() { if [[ $1 == "--" ]]; then command su "$@"; else command su -l "$@"; fi; }

However, I know it could probably stand some improvement.

(What I want this function to accomplish is rather simple. If I could use regular aliases, I'd want something like:

alias su='su -l AND alias 'su --'=su

However, I realize that the name of an alias can't have spaces...and the logic seems a bit circular...)

Does my shell function have any hidden traps? Will it negatively affect the operation of "su" if a different option is called (e.g. "su -c")?

4
  • 1
    I don't really see what the issue is. You have a shell function that works but you want an alias? That does not make any sense. The Bash manual even explicitly says "Aliases are confusing in some uses." and "For almost every purpose, aliases are superseded by shell functions."
    – Kusalananda
    Mar 29, 2023 at 4:32
  • What I want is the alias: alias su='su -l, but I also want alias 'su --'=su.
    – Digger
    Mar 29, 2023 at 4:35
  • 1
    But now you have a shell function that does exactly that instead. Is it that you want to remove the -- if the function is invoked with -- as the first argument, and then call su with the remaining arguments?
    – Kusalananda
    Mar 29, 2023 at 4:35
  • I'm worried that the shell function I am using may have a hidden trap or two...I'm lacking confidence here...
    – Digger
    Mar 29, 2023 at 4:36

1 Answer 1

1

If you call su with -- as the 1st argument, you want to execute su "$@", otherwise you want to call su -l "$@":

su () {
    if [ "$1" = -- ]; then
        command su "$@"
    else
        command su -l "$@"
    fi
}

This appears to be exactly what your function is currently doing and I see no faults in it. The only difference is that I've written it using portable POSIX syntax. The only potential issue is if you want to use su in some other way, for example su -c ... (which would be transformed into su -l -c ..., which may not be exactly what you want as it clears the environment and changes the current working directory). Use command su for these cases.

The only thing that could possibly be modified here, while retaining the original functionality of the above function, might be to reduce the number of branches that call su. This may be done by modifying the argument list before the call. The following prepends the list of arguments with -l if the 1st argument is not --:

su () {
    [ "$1" != -- ] && set -- -l "$@"
    command su "$@"
}

You would not be able to do anything similar with an alias as an alias is a plain text replacement and therefore does not have arguments that can be examined or changed in the same way as a shell function.

0

You must log in to answer this question.

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