DEV Community

Clavin June
Clavin June

Posted on

Bypassing variable using multi-line cat

TLDR:

Use 'EOF'

$ cat <<'EOF'
> $USER
> $(pwd)
> `pwd`
> EOF
$USER
$(pwd)
`pwd`
Enter fullscreen mode Exit fullscreen mode

Sometimes when we want to create a multi-line text and redirect it to the output we will use cat EOF like this

cat <<EOF > /tmp/out 
line1
line2
EOF
Enter fullscreen mode Exit fullscreen mode

and the result will be like this:

line1
line2
Enter fullscreen mode Exit fullscreen mode

but if we're gonna pass variables to the line, we're gonna see it works like this:

cat <<EOF > /tmp/out
$USER
$(pwd)
`pwd`
EOF
Enter fullscreen mode Exit fullscreen mode

output:

clavinjune
/tmp
/tmp
Enter fullscreen mode Exit fullscreen mode

if we want to keep the variables as it is we can use 'EOF' instead.

cat <<'EOF' > /tmp/out
$USER
$(pwd)
`pwd`
EOF
Enter fullscreen mode Exit fullscreen mode

output:

$USER
$(pwd)
`pwd`
Enter fullscreen mode Exit fullscreen mode

Top comments (0)