HEREDOC with fdisk

Hi folks

What I'm trying is to build a partitioning script.
I can pass a HEREDOC to fdisk just fine. Like this:

fdisk /dev/sda << EOF
p
q
EOF

but I don't know how to put that HEREDOC into a varible to pass it to fdisk.

This is what I have tried so far (no luck)

#!/bin/bash


HEREDOC_VAR_1=1


HEREDOC_VAR_2=$(cat <<EOF
p
q
EOF
)


HEREDOC_VAR_3=$(cat <<'EOF'
p
p
q
EOF
)

echo $HEREDOC_VAR_1
echo $HEREDOC_VAR_2
echo $HEREDOC_VAR_3

fdisk /dev/sda << $HEREDOC_VAR_2

echo "this works somehow but gets multiple p from former variables..."

fdisk /dev/sda << EOF
p
q
EOF

ANY Help is greatly appreciated....my head hurts from this...I just can not figure it out.
:slight_smile:

Try this:

#!/bin/bash


HEREDOC_VAR_1=1


HEREDOC_VAR_2='p
q
'


HEREDOC_VAR_3='p
p
q
'

echo $HEREDOC_VAR_1
echo $HEREDOC_VAR_2
echo $HEREDOC_VAR_3

echo "$HEREDOC_VAR_2" | fdisk /dev/sda

MAN thank you. That workzz :slight_smile:

Still curious! Is there a way to do it with a variable that holds this "kind" (heredoc) syntax?

<<EOF
p
q
EOF

I don't think so. The HEREDOCs are interpreted by the shell as the information that is to be sent through stdin to a process. It doesn't survive parameter expansion.