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.
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:
How do I parse command line arguments in Bash?
(40 answers)
How to get exact command line string from shell?
(2 answers)
Closed 3 years ago.
Suppose my script.sh could take a number of options and arguments. What is the best way to find out what the script was invoked with (form inside the script)?
For eg., someone called it with script.sh --foo_option bar_arg
Is there a way to echo that exact command they typed from inside the script?
I've tried echo !! which does not work inside a script.
This question already has answers here:
How to execute a bash command stored as a string with quotes and asterisk [duplicate]
(5 answers)
Command not found error in Bash variable assignment
(5 answers)
Closed 4 years ago.
I have an application in Unix. I use the below command to connect to it:
./application -a "connect"
I want to do the same through the shell script, for which i assigned the command line to a variable like:
newcommand = './application -a "connect"'
$newcommand
But this is not working.
However the first part of the code is working. i.e.,:
newcommand = "./application"
$newcommand
Can anyone point out what i am missing.
Believe it or not, this:
newcommand = "./application"
...has the shell run the command, newcommand with the arguments, =, and ./application.
In shell simple assignments cannot have any unprotected whitespace or they'll be interpreted as a command.
Consider:
newcommand=./application
$newcommand
...notice that there's no space around the = sign in the assignment.
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[#]}"
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.