Tools
Bash
Shell scripting, redirection, and command-line survival.
Variables
name="Ada"
echo "Hello $name"
count=$(ls | wc -l) # command substitution
readonly PI=3.14
unset nameConditionals
if [ "$x" -gt 10 ]; then
echo "big"
elif [ "$x" -eq 10 ]; then
echo "ten"
else
echo "small"
fi
[ -f file ] # file exists
[ -d dir ] # dir exists
[ -z "$s" ] # string emptyLoops
for i in 1 2 3; do echo $i; done
for f in *.txt; do echo "$f"; done
while read line; do
echo "$line"
done < file.txtRedirection & pipes
cmd > out.txtStdout to file (overwrite)cmd >> out.txtAppend stdoutcmd 2> err.txtRedirect stderrcmd &> all.txtBoth streamscmd1 | cmd2Pipe outputcmd < in.txtStdin from fileUseful commands
grep -r "text" .Recursive searchfind . -name "*.js"Find filessed "s/a/b/g" fReplace textawk "{print $1}"Print first columnxargsBuild args from stdinchmod +x script.shMake executableFunctions & args
greet() {
echo "Hi, $1" # first arg
echo "All: $@" # all args
return 0
}
greet "world"
# script args: $0 name, $# count, $? last exit code