Score:0

define variable with scope the following process

sd flag

Is it in bash scripting possible to define a variable which is scoped to the following process? Something like:

X="123" echo "$X"

Ultimately, I want to do catch the output of some process and write that (with some other surrounding text as well) to some output file:

X=$(...) echo "...$X..." > output_file
hr flag
Is there a particular reason you can't use separate statements like `X="123"; echo "$X"`?
Score:3
cn flag

In general you're right, you can set variables in the environment of the command:

VAR=value some_command_that_uses_variable_VAR

But be aware of the order of Shell Expansions:

x=123 echo "$x"

the shell will expand $x before it evaluates x=123

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.