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.
cron
is/bin/sh
unless otherwise specified by the she bang in the script. Environment variables aren’t typically set so any tilde expansion (~
) will result in access the home directory of the user that the cron job is executed under. – Allan Jun 05 '23 at 22:54dash
starts it all off. A quick reading of the man page would tell you the default shell for cron is/bin/sh
ifSHELL
isn’t set. YourSHELL
is set to the default making it redundant. The~
falls under parameter expansion for Bourne (sh), Bourne Again (Bash) and Zsh. I can’t speak to dash. – Allan Jun 06 '23 at 21:15man sh
, and it seems definitive to me. IOW, while/bin/sh
might be the default forcron
,man sh
tells us that another shell may in fact be executed instead - depending upon the various settings. Also, I tend to rely upon the local man pages instead of third-party versions. Again, the point of my answer was to *avoid speculation*. – Seamus Jun 07 '23 at 00:00cron
→/bin/sh
. That’s a given. However,/bin/sh
→ anything else if and only if it doesn’t exist. There’s no latitude for Apple there (as you put it). That’s also not an Apple thing, but the devs of the Bourne Shell - it’s that way across all platforms. – Allan Jun 07 '23 at 00:34cron
– Allan Jun 09 '23 at 12:55man cron
does not mention *any* shell at all. I can see why you're confused on this. – Seamus Jun 11 '23 at 00:06man 5 crontab
, but failed to consider the material inman sh
;man sh
, as I mentioned in a previous comment, stilldetermines howbin/sh
is executed - depending on the various settings mentioned there. And in my *Apple-sourced localman 5 crontab
, there is no"Crontab comments and variables"
section; are you using the 3rd party manuals again?* This is neither difficult, nor controversial. Given Apple's penchant for changing these settings, I feel it's more *reliable* to askcron
to tell you what shell you're using; i.e. "avoid speculation". – Seamus Jun 11 '23 at 21:18man sh
. Note reference to the file:/private/var/select/sh
. If you check your Ventura 13.4 system, you may find thatbin/bash
is actually used instead of/bin/sh
via a "soft link". Likewise for Catalina. And yes - I do contest the veracity of your 3rd party man page if it is not published by Apple (AFAIK it is not). – Seamus Jun 11 '23 at 21:31