Skip to content

Instantly share code, notes, and snippets.

@pricees
Last active April 7, 2017 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pricees/e28eafb2fd5a7331e44d212794e138a4 to your computer and use it in GitHub Desktop.
Save pricees/e28eafb2fd5a7331e44d212794e138a4 to your computer and use it in GitHub Desktop.
echo "Hello World"
echo "Hello\nWorld"
echo "\nLoad another file into context"
. load_vars
echo "Loaded \$VAR from . load_vars and its value is: $VAR"
echo "Script is also environmentally conscious: \$HOME = ${HOME}"
# Command line arguments
echo "\nArguments are context aware"
echo $1, $2, $4, $5
echo $@
echo ${@:2}
echo ${@:2:1}
read;clear
#############################################
# User entered content
printf "Enter a value: "
read value
echo "You entered: $value"
#############################################
# Variables are context aware
echo "\nFunctions and the local variables who love them"
someVar=5
echo "someVar = $someVar"
function printVar() {
local someVar=10
echo "someVar = $someVar"
}
printVar
echo "someVar = $someVar"
read;clear
###############################################
echo "\nFunctions and ... it's like Javascript all over again"
anotherVar="Hello"
echo "anotherVar = $anotherVar"
function changeVar() {
anotherVar=Hola
}
changeVar
echo "anotherVar = $anotherVar"
read;clear
###############################################
# Passing arguments
function dejaVu() {
echo "\nArguments are context aware"
echo $1, $2, $4, $5
echo $@
echo ${@:2}
echo ${@:2:1}
}
dejaVu arg1 4000 ${@:2} foo
read;clear
###############################################
# Returning values
function add() {
# return $1 + $2 WRONG!
#echo $1+$2 # prints 2000+17
echo $(($1+$2))
}
add 2000 17
sum=$(add 2000 17)
echo $sum
read;clear
###############################################
# Returning values
echo "\n\nIt's bash!"
function ls_cwd() {
ls -l | head -n 5 | tail -n 1
}
ls_cwd
# STDIN, STDOUT, STDERR
echo "\n\nStreams all the way down"
echo $(add 2000 17) | sed -e 's/17/99/g'
out=$(foo bar baz 2>&1 > /dev/null)
echo "\$out = *********$out***********"
echo "\n\nAll you need now are loops, conditions, case statements and you are ready to
rock and roll"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment