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
Related
This question already has answers here:
Why does shell ignore quoting characters in arguments passed to it through variables? [duplicate]
(3 answers)
How can I store a command in a variable in a shell script?
(12 answers)
Closed 2 years ago.
I have an external executable that I need to pass arguments to. With a bash script, I have code that determines these arguments. Some arguments may have escaped spaces.
I need to then execute that string, without expanding each of the arguments.
# ... some code that determines argument string
# the following is an example of the string
ARGSTR='./executable test\ file.txt arg2=true'
exec ${ARGSTR}
I must have the $ARGSTR be expanded so that I can pass arguments to ./executable, but each of the arguments should not be expanded. I have tried quoting "test file.txt", but this still does not pass it as one argument to ./executable.
Is there a way to do something like this?
Us an array instead of a string:
#!/usr/bin/env bash
ARGSTR=('./executable' 'test file.txt' 'arg2=true')
exec "${ARGSTR[#]}"
See:
BashFAQ-50 - I'm trying to put a command in a variable, but the complex cases always fail.
https://stackoverflow.com/a/44055875/7939871
This may achieve what you wanted :
ARGSTR='./executable test\ file.txt arg2=true'
exec bash -c "exec ${ARGSTR}"
This question already has answers here:
How to escape single quotes within single quoted strings
(25 answers)
Closed 3 years ago.
I need your help.
I am creating bash script and I have problem in part where I need to add text in line 4 of test.txt file. My text is including variable. I know how to add text with variable, but in this case this variable must be in double quotation.
So, I am using this command:
sed -i "4s/$/Username = "$var1"/g" $dir/output/test.txt
but I get next results in test.txt file:
$Username = example
I tried many options but I can not achive to get variable in double quotation:
$Username = "example"
sed -i "4s/$/Username = \"$var1\"/" $dir/output/test.txt
Global switch is meaningless and can be removed since you're appending to the end of the line
This question already has answers here:
How can I use a file in a command and redirect output to the same file without truncating it?
(14 answers)
Is it possible to modify lines in a file in-place?
(5 answers)
How to search and replace text in a file?
(22 answers)
Closed 3 years ago.
I am facing a strange issue, I have a piece of code written in groovy and contains a shell script to update the values of variables in system.properties file.
Most of the times the script works fine, but 20% of the times it deletes all the file content instead of updating the variables
def call(credentialsId, repoName, repoAddress, variableToUpdate, releaseType, BRANCH_NAME, branchToCheckoutInRepo, OS='linux'){
def now = new Date()
sh """
cat system.properties | sed -e 's#${variableToUpdate}=.*#${variableToUpdate}=${BRANCH_NAME}#g; s#releaseType=.*#releaseType=${releaseType}#g; s#lastModified=.*#lastModified="${now}"#g' | tee system.properties
"""
}```
This question already has answers here:
Lookup shell variables by name, indirectly [duplicate]
(5 answers)
How to get a variable value if variable name is stored as string?
(10 answers)
Bash - variable variables [duplicate]
(4 answers)
Closed 4 years ago.
I have a set of directories listed inside a text file as
DIR_A= (name of directory 1)
DIR_B= (name of directory 2)
....
I have a second script to which I would like to pass an argument like sh Scriptname varname where varname could be A, B, ...
Scriptname sources the initial text file and I would like this script to accept the passed varname (using $1) to echo DIR_varname.
Any ideas? TIA
This question already has answers here:
Dynamic variable names in Bash
(19 answers)
how to use variable variable names in bash script [duplicate]
(1 answer)
Closed 6 years ago.
How do I assign a value to variable that has a variable in its name?
var1="file"
var2_$var1="folder"
The code above gives me the error -bash: var2_file=folder: command not found. I was curious to know how to assign to a variable with another variable in its name.
Version of Bash is "GNU bash, version 4.1.2"
With bash you can use declare:
declare var2_$var1="123"
How about using another variable to hold the dynamic name and use it for retrieving the value after setting?
new_var=var2_$var1
declare var2_$var1="123"
echo "${!new_var}" # => 123
Unfortunately, Bash doesn't allow declare $new_var="123" - that would have made this a little prettier.