~ expansion in printf

Hi,

I have a script that at one point prints to a file as follows:

printf -- $2 > ~/.mydir/$1

The idea is to print to a hidden directory .mydir in my home directory. I've already sanitized the inputs and $1 is in the format

path1/path2/filename

and $2 is some user input.

When I run this on my Mac, there's no problem and it works great. When I upload it to the system where I eventually intend to use it, however, it doesn't work quite right. It writes the correct user input to a file located at:

my_present_directory/~/.mydir/filename

where my_present_directory is wherever I upload the script file to. It's actually making a directory called "~".

Any ideas on how to get this work work as intended?

Thanks!

~

is only guaranteed to work in interactive shells (and not even then if it's the old Bourne shell as on Solaris).

$HOME

is the correct portable way to refer to your home directory.

That worked great. Thanks!