Ask Your Question
2

Is it possible to substitute the shell variable before saving it to a file?

asked 2021-07-13 11:00:00 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-08-16 12:00:00 +0000

devzero gravatar image

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.
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-07-13 11:00:00 +0000

Seen: 9 times

Last updated: Aug 16 '21