0

Hi I have executed a function and I want to keep that values saved for further use . So what is best way to save multiple variables .My function is

test()
{
   key1='first key'
   key2='second key'
   key3='third key'
   
}
5
  • @Aaron yes exactly Commented Nov 18, 2020 at 10:16
  • 1
    what is not working? your code works fine. Did you invoke the function? run test to invoke the function, then echo $key1 shows you the value. Commented Nov 18, 2020 at 10:20
  • @ChrisMaes I want to get these in function2 as well and function2 is independent of this Commented Nov 18, 2020 at 10:37
  • The variables are not available as long as that part of code is not executed. The code is only executed when the function is called. If you want global variables to use in two different functions, you need to define them outside the two functions. Commented Nov 18, 2020 at 10:39
  • @ChrisMaes yes thats why I want to save that varibles values in a file . but can't find a way to save multiple variables in a single file in bash scripting Commented Nov 18, 2020 at 10:43

2 Answers 2

2

Eventually if you want to save variables to a file and reuse them later:

#!/usr/bin/env bash

writekeys() {
  local key1='first key'
  local key2='second key'
  local key3='third key'

  # Save variables to file
  typeset -p key1 key2 key3 >keys.sh
}

function2() {
  . keys.sh # Load variables from file
  printf 'key1=%s\n' "$key1"
  printf 'key2=%s\n' "$key2"
  printf 'key3=%s\n' "$key3"
}

writekeys

function2
Sign up to request clarification or add additional context in comments.

4 Comments

exactly what I want. just one thing if I want to update like key1 value then ?
@tempaccountas as simple as . keys.sh; key1='newvalue'; typeset -p key1 key2 key3 >keys.sh
So I getting from this , If I will run this function 2nd time it will automatically update my values ?
@tempaccountas at one point you are better experimenting with what Chris, others and I's answers. Playing with sample code and sample data will teach you more than an extended conversation with comments here. (which is not recommended and discouraged anway).
2

If you want to reuse these variables, You could define these variables globally before defining your functions:

#/bin/bash
key1='first key'
key2='second key'
key3='third key'

func1()
{
  echo $key1
}

func2()
{
  echo $key2
  key1="$key3"
}

now try executing the functions:

$ func1
first key
$ func2
second key
$ func1
third key

3 Comments

For example I Update value of $key1 in func1 and then want updated value in function 2 then ?
I have edited my answer, but next time please try yourself, this was quite obvious...
can you show your output? Try running bash -x your-script to analyze what happens.

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.