I'm trying to get a directory off of the user's home directory in a script. This is what I'm trying, but the ~
is interperated as a literal instead of expanding to the home directory. Is there anyway to get it to expand? If not, can I get the home directory another way?
$mySourceDir = "~/Projects/svn/myProject/trunk" # Single quote also does not expand
cd $mySourceDir
This is using the PS 6 beta on OSX.
To get the current user home directory, you can use the homedir() method from the os module in Node.
To navigate to your home directory, use "cd" or "cd ~" To navigate up one directory level, use "cd .." To navigate to the previous directory (or back), use "cd -" To navigate through multiple levels of directory at once, specify the full directory path that you want to go to.
On Linux it's often /home/user. However, on some OS's, like OpenSolaris for example, the path is /export/home/user.
Once you login, run cd to go to your home directory, then run pwd to print the working directory.
In PowerShell, the most robust way to refer to the current user's home directory is to use automatic variable $HOME
, inside "..."
if it is part of a larger path:
$mySourceDir = "$HOME/Projects/svn/myProject/trunk"; Set-Location $mySourceDir
(Set-Location
is PowerShell's cd
equivalent; thanks to a built-in alias definition, you can use cd
too, however.)
If you're passing a path as an argument to a command, you may be able to get away without the enclosing "..."
, depending on what characters the path contains; e.g.,Set-Location $HOME/Desktop
Works on both Windows and Unix platforms, whereas if you tried to use environment variables such as $env:HOME
, platform differences would surface.
To learn about all automatic variables (built-in variables) that PowerShell defines, see the conceptual about_Automatic_Variables
help topic (as of this writing, the description of $HOME
reflects just the Windows perspective, but $HOME
does work analogously on Unix platforms).
Use ~
only if you're certain that the current location is a filesystem location:
The current location is PowerShell's generalized concept of the current directory: PowerShell generalizes the concept of a drive to include other (typically) hierarchical data stores, such as the Windows registry, a directory of all defined functions (drive Function:
), variables (Variable
), or environment variables (Env:
).
Each such drive is provided by a drive provider, of which the filesystem [drive provider] is just one instance.
~
is a drive-provider-specific concept, so using just ~
, without an explicit reference to a drive provider, refers to the home location as defined by the provider underlying the current location.
~
represents, causing attempts to use it to fail; for instance, that is the case for the Environment
drive provider and its Env:
drive:Set-Location Env:; Set-Location ~
results in errorHome location for this provider is not set. To set the home location, call "(get-psprovider 'Environment').Home = 'path'
It is the drive provider that interprets ~
, so ~
also works inside '...'
and "..."
Set-Location ~/Desktop
Set-Location "~/Desktop"
Set-Location '~/Desktop'
bash
, where it is the shell that expands ~
, up front, before the target command sees it, but only if it is unquoted.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With