Skip to content

Instantly share code, notes, and snippets.

@odeke-em
Created November 30, 2015 00:20
Show Gist options
  • Save odeke-em/80c94e1e586cf88337c6 to your computer and use it in GitHub Desktop.
Save odeke-em/80c94e1e586cf88337c6 to your computer and use it in GitHub Desktop.
Env vars don't change within running program in various langs
package main
import (
"fmt"
"os"
)
func cwd() string {
wd, _ := os.Getwd()
return wd
}
func envPwd() string {
return os.Getenv("PWD")
}
func main() {
fmt.Println(cwd())
os.Chdir("/")
fmt.Println(cwd())
os.Chdir(envPwd())
fmt.Printf("pwdFromEnv: %s cwd: %s\n", envPwd(), cwd())
os.Chdir("/")
fmt.Printf("pwdFromEnv: %s cwd: %s\n", envPwd(), cwd())
}
function cwd() {
return process.cwd();
}
function envPwd() {
return process.env['PWD'] || '/';
}
console.log('cwd', cwd());
process.chdir('/');
console.log('new wd', cwd());
process.chdir(envPwd());
console.log('pwd %s cwd %s', envPwd(), cwd());
process.chdir('/');
console.log('pwd %s cwd %s', envPwd(), cwd());
#!/usr/bin/env python
import os
def cwd():
return os.getcwd()
def envPwd():
return os.environ.get('PWD')
def main():
print('cwd', cwd());
os.chdir('/');
print('new wd', cwd());
os.chdir(envPwd());
print('pwd %s cwd %s'%(envPwd(), cwd()))
os.chdir('/');
print('pwd %s cwd %s'%(envPwd(), cwd()))
if __name__ == '__main__':
main()
#/bin/bash
function envPwd {
echo $@ $PWD;
}
envPwd 'cwd '
cd '/'
envPwd 'new wd '
cd $OLDPWD
echo -e
envPwd 'cwd '
cd '/'
envPwd 'new wd '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment