You have created a variable named _now
, but later you reference a variable named _nowout
. To avoid such issues, use curly braces to delimit variable names:
_now=$(date +"%m_%d_%Y")
_file="${_now}out.log"
touch "$_file.txt"
Note that I have left "$_file.txt"
as is, because .
is already a variable names delimiter. When in doubt, "${_file}.txt"
could be used just as well.
Bonus 1: ${varname}
syntax actually provides several useful string operations on variables, in addition to delimiting.
Bonus 2: creative shell escaping and quoting can also be used to delimit variable names. You could quote the variable and the string literal separately (i.e. file="$_now""out.log"
or file="$_now"'out.log'
) or leave one of the parts unquoted (i.e. file=$_now"out.log"
or file="$_now"out.log
). Finally, you can escape a single character which follows your variable name: file=$_now\out.log
. Though I wouldn't recommend reusing these examples without good understanding of shell quoting and escaping rules.