2

I have a requirement to achieve in shell script.

There are two scripts script1, script2. When I run the first script two variables are created and are having some values. It will finish its execution. (the values will be right here as the script finished executing right?)

Script2 will be executed again manually later on(not sure when this script will be executed, may 30 mins or even 1 day as we have to fix something after the script1 runs).

Now I wanted to get the variables which were created in script 1 into script 2. Is that possible?

3
  • Do you need a sh-compatible answer, or is a bash-only one acceptable? (Using both tags makes that ambiguous). Commented Jan 23, 2020 at 18:17
  • Hello @CharlesDuffy, Need the answer related to shell script. Sorry I was new to this scripting part. Commented Jan 24, 2020 at 8:41
  • There's not just one "shell script" language. sh and bash are two different (POSIX-compliant) shell languages, just as C and C++ are different compiled languages. (Or, rather, sh is a name guaranteed to provide an interpreter for a POSIX sh shell, but not guaranteed to support any features not in that standard -- it may be implemented by ash, dash, or others; bash is a specific shell with a bunch of extensions that are often helpful, but which also runs much slower than many of the baseline shells). If you use the bash tag, expect an answer that works in bash but not with sh. Commented Jan 24, 2020 at 16:18

2 Answers 2

2

Do not generate content to be sourceed in or evaled unless you're prepared to deal with the security issues involved with same.

It's much safer to write a NUL-delimited stream (the same format that Linux uses to expose environment variable contents in /proc/*/environ):

tempfile=$(mktemp vars.XXXXXX)
for varname in var1 var2; do
  printf '%s=%s\0' "$varname" "${!varname}"
done >"$tempfile"
mv -- "$tempfile" varfile  # this is an atomic operation, so you can't have a half-written varfile

...and then read it back:

while IFS= read -r -d '' assignment; do
  [[ $assignment = *=* ]] || { printf 'ERROR: No = found in %q\n' "$assignment" >&2; continue; }
  var=${assignment%%=*}
  value=${assignment#*=}
  printf -v "$var" %s "$value"
done <varfile
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Charles for your help! It helped a lot! :)
1

Edit:

Charles' answer is the correct one as this can lead to code injection, but if you don't care about security (you should), then this will work.

Use source or "." and export the variables to a file

script1.sh:

var1='foo'
var2='bar'
cat > varfile <<-EOF
    var1="${var1}"
    var2="${var2}"
EOF

script2.sh:

. varfile
echo "${var1} ${var2}"

output:

$ ./script1.sh
$ ./script2.sh
foo bar
$

11 Comments

If using source or "." than you don't have to write to files, just use source for both scripts. script1 #!/bin/bash var1='foo' var2='bar' script2 #!/bin/bash echo $var1 $var2 And . ./script1; . ./script2
That would mean he needs to rerun script1 in order get the variables and he mentioned that script2 might need to be run 30 mins to a full day after
This is not a safe way to generate code. Consider if var1=$'$(rm -rf ~)\'$(rm -rf ~)\''; someone trying to dot in varfile would delete their home directory.
If you want to safely generate code that assigns two variables their existing values, then declare -p var1 var2 >varfile, or printf '%q=%q\n' var1 "$var1" var2 "$var2" >varfile
Hello @Nobody Thank you, I tried this approach but the it is giving me an error as below. Created the file using first script and reading it using second script. It worked for the first time and after that it is giving me this error. Strange! I tried grant chmod 777 permisions for this varfile but it still remains the same, no change in permissions for the file, script.sh: line 44: .: varfile: file not found
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.