I am a long time bash user, but ran into something I have never seen before (or simply don't remember). I was getting a "No such file or directory" error when trying to redirect output to a log file, something I have done thousands of times but evidently not quite in this manner. Here is a sample output that recreates the problem:
test.sh:
#!/bin/bash
logfile="~/logs/$(date +%Y%m%d_%H%M%S).log"
echo "Test" > ${logfile}
output:
$ ./test.sh
./test.sh: line 3: ~/logs/20220114_025115.log: No such file or directory
I finally isolated the problem to the ~, and the workaround to my problem is to use $HOME or hard-code the full path (boo!), but my question is why does the ~ operator not get expanded to be my home directory? I googled for an answer, but nothing quite matches my question. Thanks in advance.