Skip to content

tilde expansion in path names #1136

Closed
Closed
@StefanKarpinski

Description

@StefanKarpinski

@timholy implemented tilde_expand (see file.jl) some time ago and it is now used automatically by a few file-system commands, notably cd(). This concerns me, however, since while it's convenient for some things, it's inconsistently used, and it violates the treatment of strings as just data. What if the name of a file is just ~? We're now in a position where that name needs to be escaped and handled specially in code. On the other hand, tilde expansion is awfully handy.

So here's a possibility: bake tilde expansion into the backtick syntax instead. For example, we could make this work:

julia> run(`tail ~/.julia_history`)
           error("~user tilde expansion not yet implemented")
       end
tilde_expand("~")
tilde_expand("~/foo")
tilde_expand("~stefan/foo")
tilde_expand("~stefan")
`cat ~/.julia`
run(`cat ~/.julia`)
run(`tail ~/.julia_history`)
run(`tail $(ENV["HOME"])/.julia_history`)

Currently this just looks for a file literally named ~/.julia_history. Obviously, this would be handy for entering commands, but you could also use it in situations where you want shell-like treatment of any string. For example cd(~) would expand to cd(ENV["HOME"]) whereas cd("~") would treat its argument literally. Very importantly, tilde expansion would be done at compile time, not based on the run-time data — this is essential to avoiding nasty surprises and corner cases. In particular, interpolated values are not tilde expanded:

julia> `~`
`/Users/stefan`

julia> file = "~";

julia> `$file`
`~`

One corollary of this approach is that file and directory operations have to work with Cmd objects, which maybe ought to be renamed in light of their broadened use-cases under this proposal. Maybe they should be called ShellString objects since backtick strings are basically strings with shell-like behavior. Except that they're also not strings, so maybe that's not a great name either.

Metadata

Metadata

Labels

help wantedIndicates that a maintainer wants help on an issue or pull request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions