This question already has answers here:
How to define a shell script with variable number of arguments?
(3 answers)
Closed 4 years ago.
Currently, I'm working on a Bash script that would allow for an infinite amount of input from the user.
Currently, I have the script running only with a specific number of variables.
I'm using the following line:
read var1 var2 var3
My goal is to have it so that the user can input as many variables into the script without having to add a bunch of variables
Example of use
read -a var # see help -m read
echo "${#var[#]} inputs in array \$var with indexes: ${!var[#]}"
echo "inputs: ${var[#]}"
Related
This question already has answers here:
Dynamic variable names in Bash
(19 answers)
How to use a variable's value as another variable's name in bash [duplicate]
(6 answers)
Bash - variable variables [duplicate]
(4 answers)
Closed 2 years ago.
I recently started bash scripting and got stuck with a very basic usecase, searched stackoverflow/google but couldn't find a way to achieve what I am trying to do.
I have a script color.sh
#!/bin/bash
Apple="Red"
Orange="Orange"
Banana="Yello"
echo $$1
What I am trying to achieve is print the color of fruit and accept fruit from command line. The output I want is
./color.sh Apple -> Red, but what I get is some random number which I think is process Id.
This question already has answers here:
Bash - variable variables [duplicate]
(4 answers)
Dynamic variable names in Bash
(19 answers)
Set environment variables from file of key/value pairs
(50 answers)
Closed 2 years ago.
Here is the scenario:
The System is preset with the following Environment Variables:
INNER1=1
INNER2=2
INNER3=3
Now I have the file: values.properties
OUTER1=$INNER1
OUTER2=$INNER2
OUTER3=$INNER3
I also have a shell script named: run.sh
#!/bin/sh
PROPERTIES_FILE=values.properties
while IFS== read -r VAR1 VAR2
do
export $VAR1=$VAR2
done < $PROPERTIES_FILE
Now the problem is, when I run the script, the following is exported:
OUTER1=$INNER1
OUTER2=$INNER2
OUTER3=$INNER3
But the desired result should be:
OUTER1=1
OUTER2=2
OUTER3=3
I want it to fully resolve the variable name when exporting it.
You need to make use of shell parameter expansion.
#!/bin/sh
PROPERTIES_FILE=values.properties
while IFS== read -r VAR1 VAR2
do
export $VAR1="${!VAR2}"
done < $PROPERTIES_FILE
You may also need to change your values.properties file to:
OUTER1=INNER1
OUTER2=INNER2
OUTER3=INNER3
I hope this helps!
This question already has answers here:
Modifying a parameter pass to a script (Bash)
(3 answers)
Closed 7 years ago.
Can i change command line arguments (like $1,$2) in bash script itself?
I tried stuffs like this:
$1='a'
read var
$1=$var
The set built-in lets you do this.
From the spec:
The remaining arguments shall be assigned in order to the positional parameters.
From the bash manual:
The remaining N arguments are positional parameters and are assigned, in order, to $1, $2, … $N.
This question already has answers here:
Shell command to retrieve specific value using pattern
(3 answers)
Closed 8 years ago.
I have file test.txt contains the following
AA=testing
BB=help
CC=hello
How can i make a bash script that will get each value and assign to a new variable?
#!/bin/bash
var1=testing
var2=help
var3=hello
thanks for the help
First of all a = value is not correct syntax in shell. In shell the spaces are important.
When you have a valid file, you can use the eval function to evaluate that file as a string, or simply source it.
This question already has an answer here:
Using a variable containing spaces as a single argument
(1 answer)
Closed 8 years ago.
I am writing a bash script and i have the following:
#!/bin/bash
echo Enter some text
read tweet
t update $tweet
I have sferik t installed to tweet using the t update command and i want to pass multiple words into the $tweet variable however i am reciving this message.
ERROR: "t update" was called with arguments ["sdfs", "sdfsdf"]
Usage: "t update [MESSAGE]"
You need to quote the variable so it's passed as a single argument to t:
t update "$tweet"
In general, always quote your variables unless you know exactly why you should not.