EDIT:
My suspicion is that it's trying to expand a variable that doesn't exist. I'm almost certain that by wrapping all your variables with brackets (${TIMESTAMP_BEGIN}-${TIMESTAMP_END}
) it will work as expected.
For instance, change the code to:
# set timestamp_end:
TIMESTAMP_END=$(date +%s)
# the following uses variable expansion to expand timestamp_end
# to unknown if the variable truly is unset. Replace `:-` with :=
# if you would like to assign the value "Unknown" to timestamp_end
# if timestamp_end is null or undefined.
FILE=${DIR}/recording_${TIMESTAMP_BEGIN}-${TIMESTAMP_END:-"Unknown"}.${EXT}
mv ${TMP_FILE} ${FILE}
This will output "Unknown" if the variable TIMESTAMP_END
is really null or undefined, and you'll find out for sure if the problem is with date
or if the problem is variable expansion working differently than you'd expect it to.
ORIGINAL POST:
Is it possible that you're setting the variable in one process and checking for it in another? Variables are only accessible to their own processes. If you're running a script that sets variables they won't be accessible to your terminal after you've used them unless you use source
. I'll illustrate that with an example:
one.sh
#!/bin/bash
export onetime=$(date +%s)
echo "Time is: $onetime"
and in your terminal:
$ charlie on work-laptop in ~/test
❯❯ ./one.sh
Time is: 1469546203
$ charlie on work-laptop in ~/test
❯❯ echo $onetime
$ charlie on work-laptop in ~/test
❯❯ source one.sh
Time is: 1469546211
$ charlie on work-laptop in ~/test
❯❯ echo $onetime
1469546211
It's also important to note that if your script spawns any new processes, perhaps with a subshell for example, variables set in that new process won't be accessible outside of the process.
two.sh
#!/bin/bash
export var="[1st Value]"
echo "Var is $var"
(echo "Enter 1st subshell: "; var="[2nd Value]"; echo "=> Var is $var")
echo $var
(echo "Enter 2nd subshell: "; export var="[3rd Value]"; echo "=> Var is $var")
echo $var
and when we run this, the output is:
$ charlie on work-laptop in ~/test
❯❯ ./two.sh
Var is [1st Value]
Enter 1st subshell:
=> Var is [2nd Value]
[1st Value]
Enter 2nd subshell:
=> Var is [3rd Value]
[1st Value]