Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting home directory?

Tags:

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.

like image 394
Andy Avatar asked Jun 15 '17 19:06

Andy


People also ask

How do I find my current home directory?

To get the current user home directory, you can use the homedir() method from the os module in Node.

How do I find my home directory in Linux?

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.

How do I get to the users home directory in Linux?

On Linux it's often /home/user. However, on some OS's, like OpenSolaris for example, the path is /export/home/user.

How do I print my home directory?

Once you login, run cd to go to your home directory, then run pwd to print the working directory.


1 Answers

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.

    • Some providers provide no default for what ~ 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 error
      Home 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 "..."

    • From a filesystem location, the following commands all work the same:
      • Set-Location ~/Desktop
      • Set-Location "~/Desktop"
      • Set-Location '~/Desktop'
    • Contrast this with POSIX-like shells such as bash, where it is the shell that expands ~, up front, before the target command sees it, but only if it is unquoted.
like image 59
mklement0 Avatar answered Oct 13 '22 21:10

mklement0