You need to wrap the variables in double quotes:
echo "$newexpr" > "$path/$file"
In this case, the quotes around $path/$file not really needed, but they do not harm.
More generally, you should also use $( ) , not backlinks:
newexpr=$(awk '/^Build Number/{$4=$4+1;}1' "$path/$file")
If you want to achieve the effect of changing the file "in place", you do not need to use a variable. You can use the temporary file as follows:
awk '/^Build Number/{$4=$4+1;}1' "$path/$file" > /tmp/file && mv /tmp/file "$path/$file"
The importance of using quotes
Double quotes keep the original data format. See This simple example that uses set -x to activate debug mode. Commands executed by the shell are displayed in lines starting with + . In fact, I see that you are already using #!/bin/bash -x . set -x does the same as. :
$ s="1 > 2" $ set -x $ echo $s + echo 1 2 1 2 $ echo "$s" + echo '1 2' 1 2
The original line contains a new line, but when you echo without quotes, it is interpreted as two arguments echo instead of a single argument that contains a new line. This is called field splitting. You can learn more about the importance of using double quotes by reading this wiki article .
Tom fenech
source share