Is there a way to make julia aware of ~ as the home directory on linux

I work on a Linux system, meaning that in my mind ~ means home directory.

Sometimes I like to refer to a config file relative to a home folder in any (Linux) system. Which means that writing ~/.config/file.cfg would be very useful to me.

However Julia/ispath() doesn’t seem top recognize this alias. So the following always fails.

julia> ispath("~/a_file_in_my_home_dir.txt")
false

Is there an elegant way for me to solve this? Or do I have to resort to (pointless) things like this:

julia> ~ = homedir()
julia> isfile("$(~)/a_file_in_my_home_dir.txt")

julia> ispath(joinpath(~, "a_file_in_my_home_dir.txt"))
true

You can do

ispath(expanduser("~/a_file_in_my_home_dir.txt"))
3 Likes

Oeh, nice.

Thanks!

1 Like

I think it is useful to perceive ~ means home to bash/sh/etc…, most applications or languages do not treat ~ specially, they just appear to do because the ~ expansion happens (in the terminal) before passing the arguments to a custom command. Therefore, Julia is not alone here, on the choice of ignoring ~ leaving it to be expanded by a terminal if it called from a terminal.

Also, Julia tries to be portable, it would be bad practice to have ~ (that appears sometimes in special windows files) always expanding to the home directory.

1 Like

Hoi Henrique,

Thank you for your comment. I’m aware of the fact that bash expands tildes (and a bunch of other brackets and parameters) before passing them on. I do not expect Julia to implement this because I know this is not universally shared across all software and operating systems.

However, it is something I like to use so I was wondering if there was a way for me to still use it.

Just out of curiosity: is the meaning of .. shared across all/most software? Because this does work in Julia:

julia> isfile("../a_file.txt")
true

I think in Linux at least this should always work: each process has a “current directory”. When a process asks to open ../a_file.txt, the kernel will look for .. in this current directory. And every directory contains an entry .. which links to the parent directory. So supporting this has nothing to do with the application, it’s part of the filesystem design.

1 Like

Ah, true.

But that means it won’t necessarily work on other operating systems. Good to keep in mind.

../a_file.txt also works in Windows (10), even though directory separator is \ in Windows.
In theory, the portable / better way to write it would be joinpath("..", "a_file.txt"), but at least I had no problems with first way so far.

1 Like