4

I'm facing problem in using multiple alias in a single line on Mac bash Terminal. I have setup below alias in my .bash_profile file

alias gc=gcloud
alias cmp=compute
alias ins=instances
alias pdev="--project=devproj-12891"
function listgce(){
    gcloud compute instances list --project "$1"
}

all those are available as alias individually and can be used but when I try to use multiple of these on a single command, bash evaluates to only first one. Example :

~ % gc cmp ins list pdev
ERROR: (gcloud) Invalid choice: 'cmp'.
Maybe you meant:
  gcloud compute instances

Can please someone guide me if I'm doing something wrong in this ?

1

1 Answer 1

7

This is possible in bash: if the last character of an alias definition is a space (or tab), then bash will unalias the next word too:

$ alias foo="echo "
$ alias bar="hello "
$ alias baz=world
$ foo bar baz
hello world

Ref: 6.6 Aliases in the manual.

So you can chain aliases together: but the first word must be an alias.


I would not use aliases though: it's quite subtle and probably brittle. I'd use a function, something like

gc() {
    local -A translation=(
        [cmp]=compute
        [ins]=instances
        [pdev]="--project=devproj-12891"
    )
    local -a args
    for arg in "$@"; do
        if [[ -v "translation[$arg]" ]]; then
            args+=("${translation[$arg]}")
        else
            args+=("$arg")
        fi
    done
    echo gcloud "${args[@]}"
}

Then

$ gc cmp ins list pdev
gcloud compute instances list --project=devproj-12891

Remove the echo if it looks good.


Update to address shortcut "ci" adding 2 words "compute" "instances":

This is a special case because we don't want to add one single quoted item to the args, but two. Let me rewrite that function for greater extensibility:

gc() {
    local -a args
    for arg in "$@"; do
        case $arg in
            cmp)  args+=( compute ) ;;
            ins)  args+=( instances ) ;;
            ci)   args+=( compute instances ) ;;
            pdev) args+=( "--project=devproj-12891" );;
            *)    args+=( "$arg" ) ;;
        esac
    done
    echo gcloud "${args[@]}"
}
6
  • I want to add another shortcut "ci" for "compute instances" but when I add that and run the gcloud command, it throws me an error : 2LKMD6R ~ % g ci list poc list project poc has been set compute instances list --project=poc-proj-123 ERROR: (gcloud) Invalid choice: 'compute instances'. Maybe you meant: gcloud compute instances However, if I pass it as two arguments c and i , it translates to two argument and the command works fine is there some trick to add two words together ? Commented Dec 22, 2020 at 19:49
  • Added an update to my answer. See how it's more flexible? Commented Dec 22, 2020 at 20:58
  • Thanks very much Glenn . Other approach I found to original solution is to split words at white space args+=($=translation[$arg]) Commented Dec 22, 2020 at 21:47
  • $=? are you using zsh? Commented Dec 22, 2020 at 23:27
  • 1
    Be aware that bash and zsh are different languages. bash will not understand $=translation[$arg]. Be sure to use the appropriate shebang for your scripts. Commented Dec 23, 2020 at 19:57

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.