Skip to content

Instantly share code, notes, and snippets.

@cramhead
Last active February 11, 2019 05:44
Show Gist options
  • Save cramhead/1ebcf65ae95a452ace2596826d1a9c5c to your computer and use it in GitHub Desktop.
Save cramhead/1ebcf65ae95a452ace2596826d1a9c5c to your computer and use it in GitHub Desktop.
Notes course

Bash

Variables don't put space before or after = sign. Otherwise, it expects the next thing is a command, e.g. a=1 sleep 1; echo a is $a # a is visible to the sleep command Special chars, e.g. a space, need quotes around them, e.g. echo "some stuff" set value at time of declaration. Use unset to remove it. Prefix with $ to get value

export myvar=10 # export puts the variable in your environment 
declare -x mysecondvar=20
export -f myfunc # exports a function
echo myvar is $myvar and mysecondvar is $mysecondvar #myvar is 10
export # lists exported variables

Grouping

a=1
( 
  a=2 # shadows a
)
echo $a # prints 1
a=1
{
  a=2 # same a
}
echo $a # prints 2. Functions share variables, they don't get a copy of them

Builtins

When you type is will resolve in the following order builtins, keywords, aliases, functions

enable # lists built ins
compgen -k # list keywords

Startup

.bash_profile is read at login .bashrc is executed with each new shell Don't grow PATH with it in .bashrc, put it in .bash_profile Aliases are exported so set the in the .bashrc

source variables.sh or . variables.sh # runs a script in current process. Often to load variables or functions into the current process, without exporting them

Alias

Aliases alias commands. A different name for the same command. If you give the path to a command it will not use the aliased command, it will use the command. alias lists current aliases alias command # list's aliases for that command unalias removes the alias

Echo

echo -n # prevents the new trailing line echo -e # enables special chars like \n or \t echo -E # disable special chars

Typeset

Declares a local, to the function, variable typeset -i x=10 # typeset makes variables local to a function. -i is for integers only for perf optimization and integer operations

Declare

Declares a varible declare -l # converts all to lower case declare -u # converts all to upper case declare -r # make variable readonly declare -a myArray # allows indexing my number declare -A myAssociativeArray # allows indexing by string

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