- $0 - The name of the Bash script.
- $1 - $9 - The first 9 arguments to the Bash script. (As mentioned above.)
- $# - How many arguments were passed to the Bash script.
- $@ - All the arguments supplied to the Bash script.
- $* - All the arguments supplied to the Bash script seperated by the IFS.
- $? - The exit status of the most recently run process.
- $$ - The process ID of the current script.
- $USER - The username of the user running the script.
- $HOSTNAME - The hostname of the machine the script is running on.
- $SECONDS - The number of seconds since the script was started.
- $RANDOM - Returns a different random number each time is it referred to.
- $LINENO - Returns the current line number in the Bash script.
- create an array from multiline variables
Suites="Clubs
Diamonds
Hearts
Spades"
suite=($Suites) # Read into array variable.
num_suites=${#suite[*]} # Count how many elements.
${suite[$((RANDOM%num_suites))]} # Access random index
find the length
a='Hello World'
echo ${#a} # 11
- square brackets
if [ ]
represent thetest
command so canman test
to see options
! EXPRESSION The EXPRESSION is false.
-n STRING The length of STRING is greater than zero.
-z STRING The lengh of STRING is zero (ie it is empty).
STRING1 = STRING2 STRING1 is equal to STRING2
STRING1 != STRING2 STRING1 is not equal to STRING2
INTEGER1 -eq INTEGER2 INTEGER1 is numerically equal to INTEGER2
INTEGER1 -gt INTEGER2 INTEGER1 is numerically greater than INTEGER2
INTEGER1 -lt INTEGER2 INTEGER1 is numerically less than INTEGER2
-d FILE FILE exists and is a directory.
-e FILE FILE exists.
-r FILE FILE exists and the read permission is granted.
-s FILE FILE exists and it's size is greater than zero (ie. it is not empty).
-w FILE FILE exists and the write permission is granted.
-x FILE FILE exists and the execute permission is granted.
cut -d' ' -f 2,3 data.txt # each line of file, split on the whitespace and take the 2 and 3rd column
wc -l data.txt # count number of lines in the file
sed -ie 's/ow/aagh/g' # match globally and update file
sed 's#/a/b/c/#/d/e/f/#' # use a different delimeter
sed '11,$ d' ~/temp.txt # delete from 11th line to end of file
sed "${NUM}q;d" file # print the $NUM line of file. q will quit at that line, d will delete everything up until that line
awk '/root/ {print $1, $9;}' temp.txt # match the regex for "root" and print the first and ninth column
ls -la | awk '$10 ~/^\.s/ {gsub(/sample/, "cool"); print;}' # match the 10th column to dotfile and substitute within the matched line
- A file descriptor is a non-negative number that is used to access a file or stream.
- the file descriptor for stdin, stdout and stderr is 0, 1, and 2 respectively
So now if we want to redirect our stderr to the file we can do this:
$ ls /fake/directory 2> peanuts.txt
You should see just the stderr messages in peanuts.txt.
Now what if I wanted to see both stderr and stdout in the peanuts.txt file? It's possible to do this with file descriptors as well:
$ ls /fake/directory > peanuts.txt 2>&1
This sends the results of ls /fake/directory to the peanuts.txt file and then it redirects stderr to the stdout via 2>&1. The order of operations here matters, 2>&1 sends stderr to whatever stdout is pointing to. In this case stdout is pointing to a file, so 2>&1 also sends stderr to a file. So if you open up that peanuts.txt file you should see both stderr and stdout. In our case, the above command only outputs stderr.
There is a shorter way to redirect both stdout and stderr to a file:
$ ls /fake/directory &> peanuts.txt
Now what if I don't want any of that cruft and want to get rid of stderr messages completely? Well you can also redirect output to a special file call /dev/null and it will discard any input.
$ ls /fake/directory 2> /dev/null