I'm modifying my .bash_profile file so that when I run ipy it clears the log file every seven or more days. The problem is when ever I reference a variable within my if statement I get back -bash: command not found.

  1 #!/bin/sh
  2 # .bash_profile
 15 # These lines clear the ipy.log every seven or more days
 16 PREV=$(awk '/./{line=$0} END{print line}' days.log)
 17 echo $PREV
 18 WEEKDAY=$(date '+%Y%m%d')
 19 echo $WEEKDAY
 21 MARKER=false
 22 if [ ${DIF} > 6 ]; then
 23     YEAR=$(date '+%Y')
 24     MON=$(date '+%m')
 25     DAY=$(date '+%d')
 26     DIGIT=${DAY:-1}
 27     if [ ${DIGIT} -eq 1 ]; then
 28         TAG='st'
 29     elif [ ${DIGIT} -eq 2 ]; then
 30         TAG='nd'
 31     elif [ ${DIGIT} -eq 3 ]; then
 32         TAG='rd'
 33     else
 34         TAG='th'
 35     fi
 36     TIMESTAMP=$(date '+%H:%M:%S')
 37     MARKER=true
 38     PRINT=$("ipy.log was cleared at ${TIMESTAMP} on ${WEEKDAY} the ${DAY}${TAG}  of  ${MON},  ${YEAR}")
 39     echo ${PRINT}
 40     ${PRINT} > ~/ipy.log
 41     ${WEEKDAY} > ~/days.log
 42 else
 43     echo "It has been ${DIF} days since your last write, no need to clear logs"
 44 fi


-bash: ipy.log was cleared at 14:35:45 on 20200216 the 16th  of  02,  2020: command not found

-bash: 20200216: command not found

You'll notice that when I call upon the last line of my days.log file and assign it to the variable $PREV it only gets null assigned to it, however this is a problem that I'll formally ask in another post. I would like to figure out how and why the command not found statements are being raised.


3 Answers 3


In line 15 you:

WEEKDAY=$(date '+%Y%m%d')

So WEEKDAY == "20200216" or similar. In line 41 you:

${WEEKDAY} > ~/days.log

which will replace ${WEEKDAY} with "20200216". In bash the first string you give it is always a command (except: keywords, assignements and other things, but you can forget those for the moment). So bash will try to exectute:


which doesn't exist. You probably wanted to write:

echo "${WEEKDAY}" > ~/days.log


  • Also line 38 (but not 40). Feb 16, 2020 at 21:05

Since you use an arithmetic operation in line 20 "DIF=$((WEEKDAY-PREV))", I think the best would be if line 22 reads:

22 if [ ${DIF} -gt 6 ]; then

Also, as previously stated by Tomáš Pospíšek, line 40 as well as 41 need a command in front of the variables and therefore should read

40 echo "${PRINT}" > ~/ipy.log
41 echo "${WEEKDAY}" > ~/days.log
  • In the original version, if [ ${DIF} > 6 ]; then, the > 6 will redirect the output of the command [ ${DIF} ] to a file named "6". This is not at all close to what you want. Feb 17, 2020 at 6:35

The following line makes no sense:

PRINT=$("ipy.log was cleared at ${TIMESTAMP} on ${WEEKDAY} the ${DAY}${TAG}  of  ${MON},  ${YEAR}")

the $(...) syntax is for command substitution, so bash tries to execute:

ipy.log was cleared at ${TIMESTAMP} on ${WEEKDAY} the ${DAY}${TAG}  of  ${MON},  ${YEAR}

as a command.

Instead, you could:

  • set the variable PRINT to the string:
PRINT="ipy.log was cleared at ${TIMESTAMP} on ${WEEKDAY} the ${DAY}${TAG}  of  ${MON},  ${YEAR}"
  • echo the string to the terminal:
echo "$PRINT"
  • echo the string to a file:
echo "$PRINT" > ~/ipy.log

The assignment to PREV wasn't working as expected because ~/days.log would have been an existing but empty file, due to the mistake:

${WEEKDAY} > ~/days.log

which should have been:

echo "$WEEKDAY" > ~/days.log

Here's a slightly different approach:

# This function clears ipy.log every seven or more days
function clear_ipylog() {
    typeset -i prev yearmday dif
    typeset tag timedate message
    typeset -g marker
#     read -r prev <"$HOME/days.log" # Should work instead of awk as days.log should only contain '%Y%m%d'
    prev=$(awk '/./{line=$0} END{print line}' "$HOME/days.log")
    echo $prev >&2
    yearmday=$(date '+%Y%m%d')
    echo $yearmday >&2
    if [ $dif -gt 6 ]; then
        case ${yearmday:6:2} in
            (01) tag='st' ;;
            (02) tag='nd' ;;
            (03) tag='rd' ;;
            (*) tag='th' ;;
        timedate=$(date '+%H:%M:%S %F%b')
        message=$(printf '%s %d%s\n' "ipy.log was cleared at ${timedate:0:19}, the" \
            "${yearmday:6:2}" "$tag of ${timedate:19} ${yearmday:0:4}")
        echo "$message" > "$HOME/ipy.log"
        echo "$message" >&2
        echo $yearmday > "$HOME/days.log"
        echo "It has been $dif days since your last write, no need to clear logs"
  • This is in a function to isolate some variables – I'm assuming you don't want $prev, $dif, etc. set as global variables
  • Not sure what $marker is for, so assuming you want it set as a global var
  • ~/.bash_profile, ~/.bashrc are read directly by bash so shouldn't start with the line #!/bin/sh or have executable mode set
  • You might want to consider making ~/days.log obsolete by changing the format for the first line of ~/ipy.log so that prev can be taken from the first line/field of ipy.log

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .