Skip to content

Instantly share code, notes, and snippets.

@macshome
Created May 2, 2024 12:48
Show Gist options
  • Save macshome/f33f252f5c48d415830d720a7261d249 to your computer and use it in GitHub Desktop.
Save macshome/f33f252f5c48d415830d720a7261d249 to your computer and use it in GitHub Desktop.
A playground to see different ways to get environment variables in Swift
import Foundation
// A playground to see different ways to get environment variables in Swift
// Foundation is the easiest way using the awesome ProcessInfo class.
// Get all of the environment variables for your running process in a Dictionary.
let foundationEnv = ProcessInfo().environment
print("********** ProcessInfo Environment **********")
foundationEnv.forEach { print($0) }
// You can use subscript to just get the one you want.
if let foundationUser = foundationEnv["USER"] {
print("\n********** ProcessInfo Singe Value **********")
print(foundationUser)
}
// You can also drop down to Darwin and use the standard POSIX tools.
// Note that these are just for the POSIX environment and won't have
// all of the Foundation runtime specific values.
// Get all of the environment variables for your process like you are on a VAX.
let posixEnv = environ
// There are a lot of ways to slice and dice an array of CCHar values.
// This way is a lazy one to convert it to [UnsafeMutablePointer<CChar>?].
let bufray = Array(UnsafeBufferPointer(start: posixEnv, count: MemoryLayout.stride(ofValue: posixEnv)))
print("\n********** POSIX Environment **********")
// Old fashioned fast enumeration still works fine in Swift.
// I added an internal if-let so that there is no forced unwrapping.
for item in bufray {
if let item {
print(String(cString: item))
}
}
// Get just the variable you want like it's 1979.
let posixUser = String(cString: getenv("USER"))
print("\n********** POSIX Single Value **********")
print(posixUser)
@macshome
Copy link
Author

macshome commented May 2, 2024

Example output:

********** ProcessInfo Environment **********
(key: "SSH_AUTH_SOCK", value: "/private/tmp/com.apple.launchd.DsDeiUE66Q/Listeners")
(key: "LOGNAME", value: "josh.wisenbaker")
(key: "TMPDIR", value: "/var/folders/kf/q3kq08s500j46l9fw2knpx1m0000gp/T/")
(key: "PATH", value: "/usr/bin:/bin:/usr/sbin:/sbin")
(key: "XPC_FLAGS", value: "0x0")
(key: "DYLD_LIBRARY_PATH", value: "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks:/Users/josh.wisenbaker/Library/Developer/Xcode/DerivedData/EnvironmentVariables-guxhapkjavkxfleewpcmfslnbjet/Build/Intermediates.noindex/Playgrounds/EnvironmentVariables/Products/Debug:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib")
(key: "__CF_USER_TEXT_ENCODING", value: "0x1F6:0x0:0x0")
(key: "SHELL", value: "/bin/zsh")
(key: "USER", value: "josh.wisenbaker")
(key: "PACKAGE_RESOURCE_BUNDLE_PATH", value: "/Users/josh.wisenbaker/Library/Developer/Xcode/DerivedData/EnvironmentVariables-guxhapkjavkxfleewpcmfslnbjet/Build/Intermediates.noindex/Playgrounds/Products/Debug")
(key: "XPC_SERVICE_NAME", value: "com.apple.dt.Xcode.PlaygroundStub-macosx")
(key: "DYLD_FRAMEWORK_PATH", value: "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks:/Users/josh.wisenbaker/Library/Developer/Xcode/DerivedData/EnvironmentVariables-guxhapkjavkxfleewpcmfslnbjet/Build/Intermediates.noindex/Playgrounds/EnvironmentVariables/Products/Debug")
(key: "HOME", value: "/Users/josh.wisenbaker")
(key: "PLAYGROUND_COMMUNICATION_SOCKET", value: "/var/folders/kf/q3kq08s500j46l9fw2knpx1m0000gp/T/com.apple.dt.Xcode.pg/s/68760-0")
(key: "LOGGER_DEPTH", value: "6")

********** ProcessInfo Singe Value **********
josh.wisenbaker

********** POSIX Environment **********
XPC_SERVICE_NAME=com.apple.dt.Xcode.PlaygroundStub-macosx
PATH=/usr/bin:/bin:/usr/sbin:/sbin
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.DsDeiUE66Q/Listeners
HOME=/Users/josh.wisenbaker
__CF_USER_TEXT_ENCODING=0x1F6:0x0:0x0
TMPDIR=/var/folders/kf/q3kq08s500j46l9fw2knpx1m0000gp/T/
XPC_FLAGS=0x0
LOGNAME=josh.wisenbaker

********** POSIX Single Value **********
josh.wisenbaker

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment