Skip to content

Instantly share code, notes, and snippets.

@joshed-io
Last active December 21, 2015 06:29
Show Gist options
  • Save joshed-io/6264810 to your computer and use it in GitHub Desktop.
Save joshed-io/6264810 to your computer and use it in GitHub Desktop.
Shells and export - a quick cheat sheat / guide for beginners
# variables
# assuming you're in a file / script...
# set a variable
$ foo=1
# prove it exists
$ echo foo
# exporting a variable
# an exported variable will be available on the command line
# after you run this file
$ export foo=1
# prove it exists, even outside of the script itself
$ echo foo
# accessing files
$ / - root of filesystem
$ ~/ - home directory
$ ./ - current directory
$ ../ - up one directory
# running an executable script
# note - export does *not* have an effect when scripts are run this way
# note - often executable scripts don't have a file extension
# create a file with some editor, maybe vi.
# put #!/bin/sh at the top.
$ vi my-script
# change the permissions to be executable
$ chmod u+x my-script
# run it
$ ./my-script
# 'sourcing' a script
# if you run a script this way and use the export keyword,
# variables will become available on the command line
# some sourceable scripts end in .sh or .xxxrc but not all
# create a file with some editor, maybe vi.
# put #!/bin/sh at the top.
$ vi my-script.sh
# all of these ways to run it are equivalent
$ source my-script.sh
$ source ./my-script.sh
$ . my-script.sh
$ . ./my-script.sh
# now you can use foo
$ echo $foo
1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment