Replace a string in directory name using a variable i have a script set up to make a new directoy in folder based off a variable named for another directory that goes like this:

str="/home/folder/test"
base="$(basename $str)"

mkdir -p "/home/folder2/${base}"


What im trying to do now is find and replace a string in this varible when it makes the new folder.

Example, i have:

/Home/Album-Artist[FLAC] 


assigned to the variable.  When i use mkdir to make the new directory, I want to find and replace "FLAC" with "V0" so in the new folder it will be

/Home/Album-Artist[V0]
Solution

Replace your line:

base="$(basename $str)"


with:

base="$(basename $str | sed 's/FLAC/V0/')"


or, if you want the [ and ] brackets to become part of the search pattern:

base="$(basename $str | sed 's/\[FLAC\]/[V0]/')"


Explanation


The | (or pipe) character essentially means “take the result and feed it into the next program”.
The sed (stream editor) command takes the input, applies commands to it, and prints the result. For example, the s/FLAC/V0/ command in sed means “for each input line, search for FLAC and replace it with V0 (but no more than once)”.
sed uses regular expressions instead of fixed search patterns. That means you need to escape certain characters; hence, my last example says \[ and \] in the search pattern (instead of just [ and ]).