Shell
Variables
Section titled “Variables”$PS1: prompt$HOME: base directory$IFS: separator$0: name of the script$1,$2: arguments$*: list of arguments - truncated with first char of$IFS$@: list of arguments - as separated args$#: number of arguments$?: last return code
man bashcan explain a lot
Variable escaping
Section titled “Variable escaping”#!/bin/shx="hello world"
# a variable without quote can be two separate argumentsprintf '%s\n' $x# hello# world
# a variable with quote will be one argument (exception exists for $@)printf '%s\n' "$x"# hello world
# the $IFS char will be replaced by space# you can also change the IFS temporarly
IFS='w'printf '%s\n' $x# hello# orld
# note the space after hello and the missing 'w'
printf '%s\n' "$x"# hello world
IFS=' ' # reset the IFSRedirections
Section titled “Redirections”# Redirect stdout to FILE - overridecmd > FILE# Redirect stdout to FILE - appendcmd >> FILE
# Redirect stdout and stderr to FILEcmd &> FILE
# Redirect stderr to FILEcmd 2> FILE
# Redirect stdout to FILE and stderr to stdoutcmd > FILE 2>&1cmd >& FILE
# The standard output of cmd is connected via a pipe to the standard input of cmd2cmd | cmd2
# cmd's standard error, in addition to its standard output, is connected to cmd2's standard input through the pipecmd |& cmd2
# stdin from FILEcmd < FILE
# string as stdincmd <<< "string"echo -n string | cmd
# stdin from FILE stdout to OUTPUTcmd < FILE > OUTPUTTest command
Section titled “Test command”test -f myfile # return 1touch myfiletest -f myfile # return 0
# You can also use the test binary with the '[' binary - it's the same form# but you need a closing ']'
[ -f myfile ]