Bash alias with parameters

Asked

Viewed 760 times

2

I am creating a system that works online, the user logs in and will have access to a terminal that it can execute only the commands allowed on the server. That’s the thing about:

The user will have to run a Python script and pass parameters, so he would have to give the command:

python arquivo.py arg1 arg2 arg3

I would like to hide the Python command, I know I can configure the Bash file by adding Alias and at the moment I have already configured it like this:

alias executarArquivo='python arquivo.py arg1 arg2 arg3'

But it doesn’t work.

1 answer

3


You have some options to pass parameters. My favorite is to turn the alias into a function and treat the parameters individually:

executarArquivo() {
    python arquivo.py "$1" "$2" "$3";
}

In this option you have access to the parameters by the number, starting at $1 ($0 is the script being run).

Another way to do this is by using an alias (or function) with the variable $@:

executarArquivo='python arquivo.py "$@"'

$@ means "all parameters" - which can be very useful if you do not know how many parameters will be passed or are just creating a wrapper for another script, as is your case. Don’t forget the quotes.

An alternative to $@ is $* which basically does the same thing (takes all parameters) with the difference that $@ brings each parameter in its own quote string and $* brings them all in a single string.

  • Thank you Ricardo, that’s exactly what I needed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.