2

I have a function in bash that get param a string, for example:

MYSQL_DATABASE then I want in my file to create a var named VAR_MYSQL_DATABASE but I can`t get the value.

create_var()
{

    read -p "Enter $1 : " VAR_$1
    printf VAR_$1 // print VAR_MYSQL_DATABASE_NAME instead what I typed, so how do I get the value?
    if [[ -z VAR_$1 ]]; then
        printf '%s\n' "No input entered. Enter $1"
        create_entry $1
    fi  
}

create_var "MYSQL_DATABASE_NAME"
1
  • So I am asking from user MYSQL_DATABASE_NAME and set a variable in the bash script with the name. The idea is that I have to ask 15 variables and because of this I made this function to be dry. If I use this line read -p "Enter MYSQL_USER_NAME : " MYSQL_DATABASE_NAME all works. Commented Feb 24, 2017 at 11:21

3 Answers 3

5

Use the declare built-in in bash,

name="MYSQL_DATABASE"
declare VAR_${name}="Some junk string"
printf "%s\n" "${VAR_MYSQL_DATABASE}"
Some junk string

With the above logic, you can modify how your name variable is controlled, either present locally. If it is passed as argument from a function/command-line do

declare VAR_${1}="Your string value here"

Perhaps you want to achieve something like this,

create_var()
{
    read -p "Enter value: " value
    declare -g VAR_$1="$value"

    dynamVar="VAR_$1"

    if [[ -z "${!dynamVar}" ]]; then
        printf '%s\n' "No input entered. Enter $1"
        create_entry $1
    fi
}

So, here we are creating the dynamic variable using the declare built-in and with the dynamic part VAR_$1 cannot be referenced directly as normal variables, hence using indirect expansion {!variable} to see if the created variable is available.

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

12 Comments

Good answer ++, you may add that only allowed characters in a Unix variable name should be passed in $1
Sorry, I think I was confusing with the first piece of code, I edited the question.
you can't pass the variable and read from user at same time. Please state your requirments clearly
@anubhava: Thanks for the feedback really appreciate it!
declare makes the variable local to the function, unless you use the -g option. If it's local to the function, there's no real need for it to have any specific name.
|
3

Try the following :

#!/bin/bash

create_var() {
        declare -g VAR_$1
        ref=VAR_$1
        read -p "Enter $1: " "VAR_$1"

        echo "inside : ${!ref}"
}

create_var "MYSQL_DATABASE_NAME"
echo "output : $VAR_MYSQL_DATABASE_NAME"

declare -g will make sure the variable exists outside of the function scope, and "VAR_$1" is used to dynamically create the variable names.

Output :

Enter MYSQL_DATABASE_NAME: Apple
inside : Apple
output : Apple

6 Comments

The only thing is that echo "inside : $VAR_MYSQL_DATABASE_NAME" is not dynamic. MYSQL_DATABASE_NAME is a param. So somehow I should use echo "inside : $VAR_$1"
Yes, I know. This was just for example sake. It is actually dynamic. I'll edit my example.
@IonVasile added the example using indirection to get the value from the dynamic variable.
declare -g would be preferable to export in versions of bash that support it.
read -p "Enter $1: " "VAR_$1" is sufficient; there is no need for a command substitution here.
|
2

You can use the printf function with the -v option to "print" to a variable name.

create_var()
{
    while : ; do
        IFS= read -r -p "Enter $1 : " value
        if [[ -n $value ]]; then
            printf -v "VAR_$1" '%s' "$value"
            return
        fi
        printf 'No input entered. Enter %s\n' "$1"
    done  
}

(I've rewritten your function with a loop to avoid what appeared to be an attempt at recursion.)

Comments

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.