I\'m trying to write a bash script, which will do the following:
You need spaces around the =~
operator. Compare:
[[ foo=~bar ]]
[[ foo =~ bar ]]
This is because the first expression essentially evaluates as "Is this string empty?"
Also, the OP code uses small tilde rather than tilde.
Even so, you can easily get rid of the inner loop. Just replace the whole while read -r line2
bit with cat -- "$second_filename"
.
Your last echo $line
is only correct if the file does not end in a newline character (standard with *nix tools). Instead, you should use while read -r line || [[ $line ~= '' ]]
. This works with or without newline at the end.
Also, Use More Quotes™.