Variable containing alias not being expanded correctly

I am trying to automate minikube start process inside WSL2 Debian environment on Windows 10/11.

I have this alias already set inside the .bash_aliases

$ type minikube
minikube is aliased to `"/mnt/c/minikube/minikube.exe"'

and I have this script running on WSL2 start up:

$ cat wsl_minikube_start.sh
shopt -s expand_aliases
alias_location="${HOME}/.bash_aliases"
source "${alias_location}"
ministart="minikube start"
${ministart}
shopt -u expand_aliases

I believe it is supposed to work that way, but it shows the minikube command not found.

$ bash wsl_minikube_start.sh
wsl_minikube_start.sh: line 5: minikube: command not found

(The title was edited to make this easier to find in the future; it was not clear at the time what exactly the problem was.)


Solution 1:

Putting a command in a variable is basically always a bad idea, not only because it interferes with alias expansion (basically, aliases are parsed before variable expansions take place). See e.g. https://mywiki.wooledge.org/BashFAQ/050

Aliases are basically always a bad idea, too. Replace your alias with a function instead.

minikube () { "/mnt/c/minikube/minikube.exe" "$@"; }

Or, basically equivalently, create a wrapper script in your PATH with essentially the same contents.

#!/bin/sh
exec "/mnt/c/minikube/minikube.exe" "$@"

Returning to the first topic, it's not clear why you want to encapsulate the simple command minikube start with a variable; but if this really has some purpose, you want to use a function for that, too.

ministart () { minikube start "$@"; }

(The "$@" is only useful if you want to be able to add command-line arguments.)