如何在别名末尾使用 bash 别名?
How can I use a bash alias at the end of an alias?
是否可以在使用别名的命令末尾使用别名。
完整的命令是:
kubectl delete pod nginx --grace-period=0 --force
这些是我的别名:
alias k=kubectl
alias gpf="--grace-period=0 --force"
我想将这两个别名组合成这样:
k delete pod nginx gpf
别名不是宏;它代替了一个命令。 --grace-period=0
不是命令,“别名组合”不起作用。
此外,您的两个命令都没有定义别名。别名由
定义
alias NAME='command to execute'
您缺少 alias
关键字。因此,您对 gpf
和 k
的定义不定义别名,而是定义 shell 变量。
不过,您对 gpf
的定义还是很有用的,因为您可以在
中使用它
kubectl $gpf
以下 shell 函数具有您要求的行为:
k() {
local -a args=( kubectl ) # initialize argument list to "kubectl"
for arg do # iterate over each funtion argument
if [[ $arg = gpf ]]; then # if that argument is "gpf"...
args+=( --grace-period=0 --force ) # ...append the specific arguments we want
else
args+=( "$arg" ) # otherwise, just append what we were given
fi
done
"${args[@]}" # then run our list as a command.
}
k delete pod nginx gpf --> 这确实有效,但会引发错误。
注意的话,删除pod用不了多少时间。
错误说没有名为“gpf”的 pod,不确定 kubectl 在已经替换值时假定 gpf 是一个 pod。
是否可以在使用别名的命令末尾使用别名。 完整的命令是:
kubectl delete pod nginx --grace-period=0 --force
这些是我的别名:
alias k=kubectl
alias gpf="--grace-period=0 --force"
我想将这两个别名组合成这样:
k delete pod nginx gpf
别名不是宏;它代替了一个命令。 --grace-period=0
不是命令,“别名组合”不起作用。
此外,您的两个命令都没有定义别名。别名由
定义alias NAME='command to execute'
您缺少 alias
关键字。因此,您对 gpf
和 k
的定义不定义别名,而是定义 shell 变量。
不过,您对 gpf
的定义还是很有用的,因为您可以在
kubectl $gpf
以下 shell 函数具有您要求的行为:
k() {
local -a args=( kubectl ) # initialize argument list to "kubectl"
for arg do # iterate over each funtion argument
if [[ $arg = gpf ]]; then # if that argument is "gpf"...
args+=( --grace-period=0 --force ) # ...append the specific arguments we want
else
args+=( "$arg" ) # otherwise, just append what we were given
fi
done
"${args[@]}" # then run our list as a command.
}
k delete pod nginx gpf --> 这确实有效,但会引发错误。
注意的话,删除pod用不了多少时间。
错误说没有名为“gpf”的 pod,不确定 kubectl 在已经替换值时假定 gpf 是一个 pod。