This question already has answers here:
Indirect variable assignment in bash
(7 answers)
Closed 7 years ago.
Here is the scenario:
suppose I set positional variables:
set 1 2 3 4
eval "args_$1=something"
how do I read args_1,args_2,args_3... variables
I tried
echo $args_$1
and this also not working
eval "\$$(echo arg_$1)"
How do I get value of $arg_1, to display on terminal or pass to a function, etc.
Without eval:
$ set 1 2 3 4
$ var="args_$1"
$ declare "$var=foo"
$ echo "$var"
args_1
$ echo "${!var}"
foo
This uses an indirect variable.
$ set 1 2 3 4; eval arg_$1=koba; eval echo $`echo arg_$1`
koba
PS: Using eval is not recommended.
Related
This question already has answers here:
how to set environment variable for parent process from child process?
(1 answer)
Set environment variables in C
(5 answers)
Closed 4 years ago.
C Program: (File name: temp.c)
main ()
{
putenv ("VERSION");
setenv ("VERTSION","1.0",1);
printf ("%s\n\r",getenv ("VERSION");
exit (0);
}
output of Cprogram:
1.0
==========================================================
Bash Script:
./temp
echo "Ver from bash:"$VERSION
output of bashscript:
1.0 **(from c program)**
Ver from bash: **(from bash shell, why wont $VERSION get output here?)**
This question already has answers here:
Return value in a Bash function
(11 answers)
Closed 5 years ago.
$ function sum()
> {
> echo $1 $2;
> return $(($1+$2))
> }
$ sum 1 2
1 2
$ x=$(sum 1 2)
$ echo $x
1 2
I really expect $x to be 3. But still seems it's the "echo" result. How to fix my function?
Don't use return. Just echo the sum as:
sum() { echo $(($1 + $2)); }
Then use it as:
x=$(sum 1 2)
echo $x
3
return (or exit) value from a function is captured using $? after calling the function. However using return is not recommended as you can only return 0-255 integer values in a function.
This question already has answers here:
Check if a variable exists in a list in Bash
(21 answers)
Closed 5 years ago.
I have a bash script and I want to check if a string is in a list.
Like: string = "Hello World!", List=("foo", "bar").
Python example:
if name in list: # Another way -> if name in ["foo", "bar"]
# work to do
else:
sys.exit(1)
Thank you!
There are a number of ways, the simplest I see is:
#!/bin/sh
WORD_LIST="one two three"
MATCH="twox"
if echo "$WORD_LIST" | grep -qw "$MATCH"; then
echo "found"
else
echo "not found"
exit 1
fi
This question already has answers here:
Dynamic variable names in Bash
(19 answers)
Closed 7 years ago.
I can't find an answer to this problem, other than people asking to use an array instead. That's not what I want. I want to declare a number of variables inside a for loop with the same name except for an index number.
I=0
For int in $ints;
Do i=[$i +1]; INTF$i=$int; done
It doesn't really work. When I run the script it thinks the middle part INTF$i=$int is a command.
Without an array, you need to use the declare command:
i=0
for int in $ints; do
i=$((i +1))
declare "intf$i=$int"
done
With an array:
intf=()
for int in $ints; do
intf+=( $int )
done
Bash doesn't handle dynamic variable names nicely, but you can use an array to keep variables and results.
[/tmp]$ cat thi.sh
#!/bin/bash
ints=(data0 data1 data2)
i=0
INTF=()
for int in $ints
do
((i++))
INTF[$i]=$int
echo "set $INTF$i INTF[$i] to $int"
done
[/tmp]$ bash thi.sh
set 1 INTF[1] to data0
This question already has answers here:
Usage of :- (colon dash) in bash
(2 answers)
Closed 9 years ago.
Sorry if this is something really simple or has already been asked, but due to the nature of the question I cannot think of any search terms to put on search engines.
Lately I have seen some bash scripts that they assign variable values like this:
$ MY_BASH_VAR=${MY_BASH_VAR:-myvalue}
$ echo "$MY_BASH_VAR"
myvalue
What is the difference from the most common way of assigning a value like this:
MY_BASH_VAR=myvalue
$ echo "$MY_BASH_VAR"
myvalue
You can look at http://linux.die.net/man/1/bash
${parameter:-word} Use Default Values. If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.
This provides a default value : MY_BASH_VAR keeps its value if defined otherwise, it takes the default "myvalue"
bruce#lorien:~$ A=42
bruce#lorien:~$ A=${A:-5}
bruce#lorien:~$ echo $A
42
Suppose the $MY_BASH_VAR was already set. In this case, it will keep the same value. If not, it will get myvalue.
case 1) $MY_BASH_VAR already set.
$ MY_BASH_VAR="hello"
$ MY_BASH_VAR=${MY_BASH_VAR:-myvalue}
$ echo "$MY_BASH_VAR"
hello
case 2) $MY_BASH_VAR not previously set.
$ MY_BASH_VAR=${MY_BASH_VAR:-myvalue}
$ echo "$MY_BASH_VAR"
myvalue
case 3) $MY_BASH_VAR set to the empty string.
$ MY_BASH_VAR=""
$ MY_BASH_VAR=${MY_BASH_VAR:-myvalue}
$ echo "$MY_BASH_VAR"
myvalue