How to get the home directory from a PHP CLI script?

How to get the home directory from a PHP CLI script?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to get the home directory from a PHP CLI script?

When you are working in a terminal, typing ~ is an instant way to reference your home directory. However, translating that convenience directly into PHP code often leads developers down a rabbit hole of incorrect syntax or unexpected behavior. A common mistake involves attempting to replicate the shell's shorthand by placing the tilde character directly inside PHP string functions.

Consider the following snippet, which attempts to echo the resolved path:

#!/usr/bin/php
<?php
echo realpath(~/);
?>

This approach fails immediately because PHP interprets ~/ as a literal string containing a forward slash and a tilde. The shell expansion that occurs before the file is even handed to the script simply does not happen inside PHP strings. While you might be tempted to use realpath('~/'), this still returns a path containing the literal characters ~ rather than an absolute filesystem path like /home/username.

To resolve this, you need to either perform the shell expansion yourself or leverage built-in functions designed for environment-specific paths. The most robust solution involves using the getenv() function combined with the HOME environment variable. This method bypasses the ambiguity of string interpolation entirely.

#!/usr/bin/php
<?php
$homeDir = getenv('HOME');
echo $homeDir; 
?>

This code retrieves the value of the HOME environment variable, which is set by the operating system when a user logs in. If the script lacks execute permissions or is not run within an interactive shell session where the environment is loaded, getenv() might return false or an empty string. In such edge cases, it is prudent to provide a fallback, perhaps checking for $HOME as a superglobal variable or defaulting to /root on Linux systems if necessary.

Alternatively, you can use realpath('~/') only after ensuring the tilde is expanded first. You could achieve this by manually replacing the tilde with the actual path obtained from getenv('HOME'), though this adds unnecessary complexity if getenv() is already available. The core principle remains: PHP does not automatically expand shell variables, so you must handle the expansion logic explicitly.

Another angle to consider is the distinction between CLI and Web contexts. In a web server environment like Apache or Nginx running PHP via FastCGI, the HOME variable might not be set at all depending on how the process manager spawns the worker. Conversely, when running directly as a CLI script (as in your example), the environment is usually inherited from the terminal session, making getenv('HOME') reliable.

If you are using Laravel or similar frameworks, you might encounter scenarios where the application runs via Swoole or RoadRunner, which could alter how environment variables are passed to the worker processes. While these frameworks provide their own ways to handle configuration, relying on standard PHP functions like getenv() ensures consistency across different execution environments without depending on framework-specific APIs that may change.

Ultimately, the choice of method depends on your specific deployment architecture. For a simple script needing the current user's home directory, getenv('HOME') is the standard approach. It is concise, readable, and leverages the operating system's own definition of the user's home location, avoiding the pitfalls of manual string manipulation or shell-specific shorthand.

Our network