Convert string to bash argument [duplicate] - linux

This question already has an answer here:
Bash missing $(HOME) variable
(1 answer)
Closed 5 years ago.
i want to know if there is a way to do something like this
VAR="-o sampleoutput"
./mycommand.sh $(VAR)
Thank you
Edit: This question is different. When i tried to run my command withuot parenthesis too but it did not work. Here is my code
ADDITIONAL_ARGUMENTS="-J-javaagent:/opt/newrelic/newrelic.jar &"
./bin -Dconf=./configfile $ADDITIONAL_ARGUMENTS
This is my result
Bad root server path: /path/to/my/project/&

Remove your () around VAR.
VAR="-o sampleoutput"; ./mycommand.sh $VAR

Related

How can we execute a string command and then get the result of it to assign a variable in shell script bash [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 12 months ago.
I want to execute a string command and then get the result of it to assign a variable in shell script bash.
for example :
strCode="scontrol show jodid --dd $VALUE"
eval $strCode
The point here is this codepart worked but how can I get the results from the `
eval
` command and assign it to a variable, because I need it to use.
when I type the theResult=eval $strCode it does not work.
thanks for the help
The solution is:
strCode=$(scontrol show jodid --dd $VALUE)
echo "${strCode}"
It's easier than you are doing:
theResult=$(scontrol show jodid --dd $VALUE)
You should follow some bash tutorial, as this is a quite basic question.

How to use variables in brace expansion [duplicate]

This question already has answers here:
How do I iterate over a range of numbers defined by variables in Bash?
(20 answers)
Closed 1 year ago.
This works as expected:
$ echo file-{00..03}
file-00 file-01 file-02 file-03
This does not do what I wanted:
$ start=00
$ end=03
$ echo file-{$start..$end}
file-{00..03}
The reason is that brace expansion is performed before any other epansion.
In my case, the sequence limits are in the variables start and end.
Any clever way to hack my way around this?
I have this:
$ eval echo file-{$start..$end}
file-00 file-01 file-02 file-03
But I am open to less ugly suggestions.

bash string manipulation got errors in jenkins shell (not pipeline) [duplicate]

This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 3 years ago.
i am trying to get value of variable by given string:
Running this code on Jenkins and its get "bad substitution"
in regular shell it works.
example:
param1="hello"
param2="world"
PARAMS="param1 param2"
for p in $PARAMS;do
echo ${!p}" "
done
what the best way to make it work in Jenkins too.
You must use with $ sign before the name of the variable:
PARAMS="$param1 $param2"
for p in $PARAMS;do
echo ${p}" "
done

Is there any difference in how variables are referenced in shell script? [duplicate]

This question already has answers here:
When do we need curly braces around shell variables?
(7 answers)
Closed 3 years ago.
Consider the following code:
name=John
echo ${name}
It prints "John", just as expected. Now consider this code:
name=John
echo $name
Again, this code prints "John" just as expected. Both codes work fine.
But I wonder is there any difference between the two, e.g. compatibility?
In your case, there is no difference.
In this case, there is:
name=John
echo ${name}Doe
echo $nameDoe
Read more: here

expand unix variable inside sed command [duplicate]

This question already has answers here:
Replace a string in shell script using a variable
(12 answers)
sed substitution with Bash variables
(6 answers)
Closed 4 years ago.
I need to replace current value in configuration file with new value which is assigned to variable ,
like
file_name=abc.txt
needs to be replaced like
file_name=xyz.txt
where $file=xyz.txt
I tried
sed -i 's/file_name=.*/file_name=$file/g' conf_file.conf
however the variable is not getting expanded,
it comes like file_name=$file.
any pointers?
This should work,assuming that variable file has value:xyz.txt assigned to it:
sed "s/file_name=.*/file_name=${file}/g" file_name
Output:
file_name=xyz.txt

Resources