Some shells support ~
, and some don't. Perhaps Apple uses dash
as the default cron
shell in Catalina, and bash
in Monterey?; perhaps Apple changes the cron
environment depending upon the phase of the moon?
Better than guessing what Apple has (or has not) done on some particular version of macOS, and on a machine that may or may not have user modifications... wouldn't it be better to run some simple tests - ask cron
to tell you something about its environment? This would be an objective truth; one that requires no assumptions, and is not dependent upon vagaries in Apple's documentation.
Once a day (or whenever you wish), you can run a cron
job that logs its environment and some other useful information from your system to a file. The file is always there, and its never more than 24 hours old (assuming your system was operating at the time you scheduled it).
A job similar to the one below in your crontab
would answer your questions "definitively" (however see NOTE 3. below):
0 12 * * * (echo $(date); echo "What is '~'?:" ~; printenv) > /Users/seamus/mycronenv.txt 2>&1
This produces a file named mycronenv.txt
in my $HOME directory that looks similar to this:
Sat Jul 30 23:35:00 CDT 2022
What is '~'?: /Users/seamus
SHELL=/bin/sh
MAILTO=
USER=seamus
PATH=/usr/bin:/bin
PWD=/Users/seamus
SHLVL=1
HOME=/Users/seamus
LOGNAME=seamus
_=/usr/bin/printenv
As you can see, my system's (Catalina) cron
recognizes ~
as $HOME. To verify this, I added another job to my crontab
(see NOTE 1. below):
* * * * * (echo "Write this line to the file ~/testdir/testfile with a timestamp: "; echo $(date)) >> ~/testdir/testfile.txt 2>&1
Which worked as expected, writing the result to $HOME/testdir/testfile.txt
.
And since I used the append form of the redirect (>>
), it adds a new entry every minute - i.e. remove this job after your test!
Hope this helps clear the confusion - please let us know if the issue persists, or you have other questions.
NOTES:
If you want to try this on your machine, create the directory testdir
first:
% cd
% mkdir testdir
On my system (Catalina), Apple has set the default configuration to send mail to me with a note re my cron
job - which is not something I want. Consequently, I have added a line to my crontab
to disable that:
MAILTO=""
Wrt definitively:
From man sh
it seems that, rather than being definitive, Apple has given themselves some latitude: cron
may use any one of three shells (bash
, dash
or zsh
) in service of /bin/sh
:
sh is a POSIX-compliant command interpreter (shell). It is implemented by re-execing as either bash(1), dash(1), or zsh(1) as determined by the symbolic link located at /private/var/select/sh. If /private/var/select/sh does not exist or does not point to a valid shell, sh will use one of the supported shells.