help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-bash] The best way to derefrence a variable whose name is stored i


From: Peng Yu
Subject: [Help-bash] The best way to derefrence a variable whose name is stored in another variable.
Date: Fri, 24 Apr 2015 14:42:26 -0500

Hi I use the following code to test if the variable name stored in "x"
actually have any value set. But I am not sure if it is the best way
to do.

Does anybody know what is the best way to do so in bash?

#!/usr/bin/env bash

var=10
x=var

if [ "$(eval echo "\$$x")" ]
then
  echo "the variable '$x' has a value"
else
  echo "the variable '$x' has no value"
fi


x=nvar

if [ "$(eval echo "\$$x")" ]
then
  echo "the variable '$x' has a value"
else
  echo "the variable '$x' has no value"
fi



-- 
Regards,
Peng



reply via email to

[Prev in Thread] Current Thread [Next in Thread]