0

I have (roughly) the following code:

read -p "What is your variable: " WHATIS

cat > ./test.txt <<EOF

This should be a bunch of lines
I want this "variable" to be imbedded
it is ${WHATIS}

EOF

I keep getting the literal output of "${WHATIS}.
I did this ages ago but can not remember how or find the code I used.
Any help would be appreciated

Edited by ennoil: Typo

2
Contributors
1
Reply
9
Views
1 Year
Discussion Span
Last Post by chriswelborn
0

All three of these options are working for me:

Plain echo "string" > file:

read -p "What is your variable: " WHATIS

echo "This should be a bunch of lines
I want this \"variable\" to be embedded
it is $WHATIS" > ./test.txt

cat with HEREDOC and variable expansion:

read -p "What is variable 2?: " WHATIS2

cat > ./test.txt <<EOF

This should be a bunch of lines
I want this "variable" to be embedded
it is $WHATIS2

EOF

cat withHEREDOC and variable substitution/expansion:

read -p "What is variable 3?: " WHATIS3

cat > ./test.txt <<EOF

This should be a bunch of lines
I want this "variable" to be embedded
it is ${WHATIS3}

EOF

Using ${var} instead of $var is for when you want the variable name separated from other content (like echo "${myvar}s are cool"), or when you are actually doing some extra substitution (like echo "${myvar:-mydefault}"). It doesn't hurt to use ${}, but it's not necessary.

Edited by chriswelborn: explanation

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.