cat EOF 保持缩进并解释变量

cat EOF keeps indenting and interprets variables

使用'EOF',可以保持缩进,但不能解释变量

[root@master ~]# registry=10.105.100.4/library
[root@master ~]# cat > /etc/sysconfig/kubelet << 'EOF'
> KUBELET_EXTRA_ARGS=--pod-infra-container-image=${registry}/pause:3.1 \
>                    --runtime-cgroups=/systemd/system.slice --kubelet-cgroups=/systemd/system.slice \
>                    --feature-gates=LocalStorageCapacityIsolation=true \
>                    --kube-reserved-cgroup=/kubepods.slice --kube-reserved=cpu=500m,memory=500Mi,ephemeral-storage=1Gi \
>                    --system-reserved-cgroup=/system.slice --system-reserved=cpu=500m,memory=500Mi,ephemeral-storage=1Gi \
>                    --eviction-hard=memory.available<500Mi,nodefs.available<10% \
>                    --max-pods=250
> EOF
[root@master ~]# cat /etc/sysconfig/kubelet
KUBELET_EXTRA_ARGS=--pod-infra-container-image=${registry}/pause:3.1 \
                   --runtime-cgroups=/systemd/system.slice --kubelet-cgroups=/systemd/system.slice \
                   --feature-gates=LocalStorageCapacityIsolation=true \
                   --kube-reserved-cgroup=/kubepods.slice --kube-reserved=cpu=500m,memory=500Mi,ephemeral-storage=1Gi \
                   --system-reserved-cgroup=/system.slice --system-reserved=cpu=500m,memory=500Mi,ephemeral-storage=1Gi \
                   --eviction-hard=memory.available<500Mi,nodefs.available<10% \
                   --max-pods=250

就用EOF,可以解释变量,但是不能一直缩进,

[root@master ~]# registry=10.105.100.4/library
[root@master ~]# cat > /etc/sysconfig/kubelet << EOF
> KUBELET_EXTRA_ARGS=--pod-infra-container-image=${registry}/pause:3.1 \
>                    --runtime-cgroups=/systemd/system.slice --kubelet-cgroups=/systemd/system.slice \
>                    --feature-gates=LocalStorageCapacityIsolation=true \
>                    --kube-reserved-cgroup=/kubepods.slice --kube-reserved=cpu=500m,memory=500Mi,ephemeral-storage=1Gi \
>                    --system-reserved-cgroup=/system.slice --system-reserved=cpu=500m,memory=500Mi,ephemeral-storage=1Gi \
>                    --eviction-hard=memory.available<500Mi,nodefs.available<10% \
>                    --max-pods=250
> EOF

[root@master ~]# cat /etc/sysconfig/kubelet
KUBELET_EXTRA_ARGS=--pod-infra-container-image=10.105.100.4/library/pause:3.1                    --runtime-cgroups=/systemd/system.slice --kubelet-cgroups=/systemd/system.slice                    --feature-gates=LocalStorageCapacityIsolation=true                    --kube-reserved-cgroup=/kubepods.slice --kube-reserved=cpu=500m,memory=500Mi,ephemeral-storage=1Gi                    --system-reserved-cgroup=/system.slice --system-reserved=cpu=500m,memory=500Mi,ephemeral-storage=1Gi                    --eviction-hard=memory.available<500Mi,nodefs.available<10%                    --max-pods=250
[root@master ~]# 

有什么办法可以同时生效吗?

您必须转义行尾的反斜杠(使用 bash 和 zsh 进行测试;我假设其他 shell 类似):

$ cat > foo.txt <<EOF
line 1 \
 line 2
EOF
$ cat foo.txt
line 1 \
 line 2