Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I would like to test if the name of a variable given as argument to a function contains a certain string, and do different actions if it does/does not.
Here is a very basic description of what I want to achieve:
#!/bin/bash
varA="a"
varB="b"
varC="c"
varD="d"
varE="e"
varF="f"
function1 ()
if name of argument $1 does not contain "A"; then
echo "no A in $1"
further actions done using the value of varA and others...
elif name of argument $1 contains "A"; then
echo "A in $1"
further actions done using the value of varA and others...
function1 $varA $varB $varC
function1 $varD $varE $varF
And when running this would give
A in varA
no A in varD
I saw while doing some research that the name of a variable could be referred to as ${!var@}, so here is the code I tried:
1 #!/bin/bash
3 varA="a"
4 varB="b"
5 varC="c"
6 varD="d"
7 varE="e"
8 varF="f"
10 function1 ()
12 if [[ "${!1@}" != *"A" ]]; then
13 echo "no A in $1"
14 elif [[ "${!1@}" == *"A" ]]; then
15 echo "A in $1"
16 fi
19 function1 $varA $varB $varC
20 function1 $varD $varE $varF
But when running the script it gives the following error messages:
./testx.sh: line 12: ${!1@}: bad substitution
./testx.sh: line 12: ${!1@}: bad substitution
Any help would be much appreciated! Thank you
–
–
–
–
–
If you want to "test if the name of a variable" then give that to the function:
function1 varA
Not the contents of the variable:
function1 $varA # INCORRECT
Then, you could test the name of the variable inside the function:
if [[ $1 == *A* ]]; then
The script could be as this one:
#!/bin/bash
function1 ()
if [[ $1 == *A* ]]; then
echo "A in $1"
echo "no A in $1"
function1 varA varB varC
function1 varD varE varF
IF you need "the value of the var" inside the function, then just use this:
value=${!1}
That is called "indirect expansion" inside the man bash
.
After that, value
will contain a copy of the variable named in $1
.
To change the value of the variable pointed by $1
, use this:
declare $1=newvalue
Or, quite insecure if the value of $1 could be set by an external user:
eval $1=newvalue
The use of ${!var*}
(or ${!var@}
) is to get a list of all variables that use a name that start with var
. Not to reference a particular variable.
$ eval var{A..F}=1 # create the list of variables
$ echo ${!var*} # use the expansion to list variables
varA varB varC varD varE varF
The expansion of ${!1*}
should have been the list of variables that start with a 1
. Those variable names are invalid (except as positional parameters) in bash. That is: variable names could not start with a number.
That expansion is (therefore) an error, and reported as so by bash.
–
–
–
Be aware the script you are looking for checks only for the first variable of the function call (i.e. $1). So calling for function1 $varA $varB $varC
would only check for varA
The code below would do the work you expect under the pseudo code you have written:
#!/bin/bash
varA="checkAinvariable"
varB="b"
varC="c"
varD="d"
varE="e"
varF="f"
function1 ()
if [[ "$1" != *"A"* ]]; then
echo "no A in $1"
elif [[ "$1" == *"A"* ]]; then
echo "A in $1"
function1 $varA $varB $varC
function1 $varD $varE $varF
Output would be:
A in checkAinvariable
no A in d
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.