I have a script that has a $variable
storing a text with an undetermined number of lines. Let's say I've named it script.bash
and it's located in $HOME
:
#!/bin/bash
# commands on the beginning of my script
variable='Lorem Ipsum is simply dummied
text of the printing and
typesetting industry. Lorem Ipsum has been the
industry standard dummy text ever since the 1500s'
# commands at the end of my script
I'd like to create a new script that will change the text in variable
on script.bash
. The idea of the code would be something like this:
#!/bin/bash
file="$HOME/script.bash"
mynewvariable='one
two
three'
substitute_everything_inside_variable () {
# code
}
substitute_everything_inside_variable "$mynewvariable" "$file"
As long as it was only a single line it could work substituting the entire row by a new one using sed
like the following:
sed -i "5s/.*/$mynewvariable/" "$HOME/script.bash"
But considering I need to substitute everything inside the single quotes (and the number of lines of the text inside it is variable) I don't see how I could do it using command-line tools. Is there a way of doing it without reading and interpreting what happens in every line?
$variable
into a file (e.g.vfile.txt
) and then having your shell script do something likevariable="$(cat vfile.txt)"
? It's much easier to replace the entire contents of a file than to change a multi-line quoted string inside a file with other stuff you don't want to break.