Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Yes, it is possible to substitute a shell variable before saving it to a file using command substitution.

For example:

#!/bin/bash

name="Alice"
file_content="Hello, my name is $(echo $name)."

echo "$file_content" > greeting.txt

In this script, the variable $name is substituted using command substitution $(echo $name) before saving the string to the file greeting.txt. The contents of greeting.txt would be:

Hello, my name is Alice.