Bash alias with parameters

1

I'm creating a system that works online, the user logs in and will have access to a terminal which can execute only the commands allowed on the server. The question is this:

The user will have to execute a script in Python 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 that I can configure the Bash file by adding Alias and at the moment I have already configured it:

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

But it does not work.

    
asked by anonymous 02.08.2017 / 09:22

1 answer

1

You have some options for passing parameters. My favorite is to transform 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 with $1 ( $0 is the script that is running).

Another way to do this is to use 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 you are just creating a wrapper for another script, as is your case. Do not forget the quotes.

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

    
02.08.2017 / 12:09