Last active
December 21, 2015 06:29
-
-
Save joshed-io/6264810 to your computer and use it in GitHub Desktop.
Shells and export - a quick cheat sheat / guide for beginners
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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