8

I can redirect the output and then cat the file and grep/awk the variable, but I would like to use this file for multiple variables.

So If it was one variable say STATUS then i could do some thing like

echo "STATUS $STATUS" >> variable.file
#later perhaps in a remote shell where varible.file was copied
NEW_VAR=`cat variable.file | awk print '{$2}'`

I guess some inline editing with sed would help. The smaller the code the better.

2 Answers 2

11

One common way of storing variables in a file is to just store NAME=value lines in the file, and then just source that in to the shell you want to pick up the variables.

echo 'STATUS="'"$STATUS"'"' >> variable.file
# later
. variable.file

In Bash, you can also use source instead of ., though this may not be portable to other shells. Note carefully the exact sequence of quotes necessary to get the correct double quotes printed out in the file.

If you want to put multiple variables at once into the file, you could do the following. Apologies for the quoting contortions that this takes to do properly and portably; if you restrict yourself to Bash, you can use $"" to make the quoting a little simpler:

for var in STATUS FOO BAR
do
    echo "$var="'"'"$(eval echo '$'"$var")"'"'
done >> variable.file
Sign up to request clarification or add additional context in comments.

5 Comments

@Amadan It's portable to any POSIX compliant shell; Bash, dash, ksh, zsh, etc.
Indeed. But not csh, tcsh.
@Amadan When referring to portability among shells, one generally means portability between shells that adhere to the POSIX standard (or come close). Of course the same constructs won't be portable to non-standard shells, or shells that conform to a different standard. There are plenty of other shells, like cmd.exe, PowerShell, and so on that this won't be portable to; those are entirely different languages, as opposed to variants of the same language.
thats cool, but how on earth would you update a variable inside your file?? read and write all of its contents again? :/
@Blauhirn Yes. For small config files like this, there's not really any more efficient way to do it; the whole file will probably fit into a single block so editing the whole file vs just the part that needs to change takes the same amount of time. And if you're updating some large database where efficiency is important, you probably shouldn't be doing so from a shell script anyhow.
9

The declare builtin is useful here

for var in STATUS FOO BAR; do
    declare -p $var | cut -d ' ' -f 3- >> filename
done

As Brian says, later you can source filename

declare is great because it handles quoting for you:

$ FOO='"I'"'"'m here," she said.'
$ declare -p FOO
declare -- FOO="\"I'm here,\" she said."
$ declare -p FOO | cut -d " " -f 3-
FOO="\"I'm here,\" she said."

2 Comments

Ah, nice. I didn't know about declare -p. Looks like it's not standard, but would work if you're restricting yourself to Bash and zsh. You probably don't need to strip off the declare using cut if you're going to be feeding it to Bash too; the declare statement as printed by declare -p should work to set the variable.
thats cool, but how on earth would you update a variable inside your file?? read and write all of its contents again? :/

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.