防止函数参数中的变量扩展

Prevent variable expansion in the parameter of a function

function kubeall { 
    for i in `seq 0 2`; do 
        echo pod-$i
        kubectl exec -it pod-$i -- bash -c "$@"
    done 
}
kubeall "cat ~/logs/pod-$i/log.out"

是否可以在参数本身中防止变量(在本例中为$i)的扩展?

按原样将 $i 传递给 kubeall 无济于事。您应该将 $i 作为位置参数传递给 bash。

function kubeall {
  for i in {0..2}; do
    echo "pod-$i"
    kubectl exec -it "pod-$i" -- bash -c "" bash "$i"
  done
}
kubeall 'cat ~/"logs/pod-/log.out"'

不确定这是否是您想要的,但它是这样的:

#!/bin/bash
function kubeall {
    echo '$@: '"$@"
    for i in $(seq 0 0); do
        echo pod-$i
        #Note :: Include $i to the new shell.
        bash -c "i=$i; $@"
    done
}

#Note :: Using single quotes here to send the arguments as it is.
kubeall 'echo ~/pod-$i/log.out'

输出:

$@: echo ~/pod-$i/log.out
pod-0
/home/username/pod-0/log.out