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
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
When you type is will resolve in the following order builtins, keywords, aliases, functions
enable # lists built ins
compgen -k # list keywords
.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
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 -n
# prevents the new trailing line
echo -e
# enables special chars like \n or \t
echo -E
# disable special chars
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
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