lewiatan
lewiatan

Reputation: 1176

Use bash alias name in a function that was called using that alias

I'm playing a game in a terminal. It uses ssh connection and have a different pair of user and password for each level.

I use my bash aliases to store those pairs, i.e. I have an alias for each level where I call a bash function with 2 parameters: password and number of level.

function log.as.bandit() {
    sshpass -p $1 ssh [email protected]
}
alias bandit14="log.as.bandit secretPass 14"
alias bandit15="log.as.bandit differentSecretPass 15"

It would be even easier for me if I could pass as a parameter only password and take the username from an alias that I used.

Question:
Is that possible to use alias name in a function that has been called by that alias?

In the example:

function log.as.bandit() {
    sshpass -p $1 ssh [email protected]
}
alias bandit14="log.as.bandit secretPass"
alias bandit15="log.as.bandit differentSecretPass"

Upvotes: 2

Views: 546

Answers (1)

melpomene
melpomene

Reputation: 85767

As far as I know: no, you can't do that with aliases.

But what you can do is:

function log.as.bandit() {
    sshpass -p "$1" ssh "${FUNCNAME[1]}"@bandit.labs.overthewire.org
}
bandit14() { log.as.bandit secretPass; }
bandit15() { log.as.bandit differentSecretPass; }

i.e. use functions instead of aliases.

FUNCNAME is an array containing the names of the currently executing functions. ${FUNCNAME[0]} is the name of the current function itself (log.as.bandit); ${FUNCNAME[1]} is the name of the calling function (bandit14 or bandit15).

Upvotes: 6

Related Questions