0

I am pretty new to the world of bash. I am trying to achieve one thing, that is to have a more easily readable form of a command.

For example: I usually need to do su - <user>

What I want to do is type changeto <user>, and then when I press enter in the backend the actual command su - <user> should be executed.

4
  • There seems to be two question here. You can only ask one question per question. You can ask as many as you like. I stripped off the 2nd one. Feel free to ask in another question, but I feel that people may think that you have not done any research. Add what you have tried, and what happened. Commented Dec 11, 2019 at 20:21
  • I don't get it: How is changeto shorter than su -? And what back\end? did you miss out something important? Commented Dec 11, 2019 at 20:24
  • well I am going for more like human readable language kind of thing. I can easily catchon if i know how to do one. There are other long commands that I use on daily basis.
    – JackyBoi
    Commented Dec 12, 2019 at 17:17
  • I edited your question (You can also do this), to make it say what you said in your comment. Commented Dec 12, 2019 at 21:23

1 Answer 1

0

You could do this with an alias or with a simple shell function.

With an alias:

alias changeto='su -'

With a shell function:

changeto () {
    su - "$@"
}

Both of these would behave the same when used on the command line.

You would put either of these (but not both) in whatever place that you usually put alias definitions, most likely into ~/.bashrc or ~/.bash_aliases if you are using bash as your interactive shell. The alias or shell function will be available in the next shell that you start (for example if you open a new terminal).

If using the alias, then the shell would simply replace the word changeto with su - when you use that command. Any further arguments that you use (a username, for example) will be tagged on to the end of that.

If using the shell function, the shell would call su - followed by whatever other arguments you pass to the function. In this case, the effect would be the same as with the alias.

You must log in to answer this question.

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