Bash
From charlesreid1
Bash Guide
Using Bash
Setting the default shell: http://www.unix.com/shell-programming-scripting/32664-how-change-default-shell-linux.html
Startup Process
Dot Files
Bash Scripting
Looping
Looping in bash is really simple, and convenient - you can use other unix commands (most obviously "ls") to create lists, and then loop over each element of those lists. For example, if I want to print out the name of every file in my home directory (granted, not very useful, but this is just an example), I can do this:
for i in `/bin/ls -1 $HOME`; do
echo $i
done
To do some serious bash looping kung-fu, check out Xargs.
Logical Operators
NOTE: the use of double brackets for logical condition checks is highly recommended.
There are several logical operators available for checking conditions in bash.
These can be tested using a simple "if" statement. In bash, if statements are of the form:
if [[ condition ]]; then
cmd
else
cmd2
fi
The spaces between the brackets and the condition are essential.
Integer Operators
| Operator | Meaning | Example |
|---|---|---|
-eq
|
returns true if arguments are equal | $ a=1; b=1; if [[ "$a" -eq "$b" ]]; then echo "true"; else echo "false"; fi
true
$ a=1; b=2; if [[ "$a" -eq "$b" ]]; then echo "true"; else echo "false"; fi
false
|
-ne
|
returns true if arguments are not equal | $ a=1; b=1; if [[ "$a" -ne "$b" ]]; then echo "true"; else echo "false"; fi
false
$ a=1; b=2; if [[ "$a" -ne "$b" ]]; then echo "true"; else echo "false"; fi
true
|
-gt
|
returns true if argument 1 is greater than argument 2 | $ a=5; b=10; if [[ "$a" -gt "$b" ]]; then echo "true"; else echo "false"; fi
false
|
-ge
|
returns true if argument 1 is greater than or equal to argument 2 | $ a=15; b=10; if [[ "$a" -ge "$b" ]]; then echo "true"; else echo "false"; fi
true
$ a=10; b=10; if [[ "$a" -ge "$b" ]]; then echo "true"; else echo "false"; fi
true
|
-lt
|
returns true if argument 1 less than argument 2 | $ a=5; b=10; if [[ "$a" -lt "$b" ]]; then echo "true"; else echo "false"; fi
true
|
-le
|
returns true if argument 1 less than or equal to argument 2 | $ a=15; b=10; if [[ "$a" -le "$b" ]]; then echo "true"; else echo "false"; fi
false
$ a=10; b=10; if [[ "$a" -le "$b" ]]; then echo "true"; else echo "false"; fi
true
|
\<
|
returns true if argument 1 less than argument 2
this needs to be escaped with a \ if it occurs inside single brackets; otherwise it can appear between double bracket or between double parentheses |
$ a=10; b=10; if [ "$a" \< "$b" ]; then echo "true"; else echo "false"; fi
false
$ a=10; b=10; if [[ "$a" < "$b" ]]; then echo "true"; else echo "false"; fi
false
$ a=10; b=10; if (( "$a" < "$b" )); then echo "true"; else echo "false"; fi
false
|
<=
|
returns true if argument 1 less than or equal to argument 2
this operator needs to go inside double parentheses |
$ a=10; b=10; if (( "$a" <= "$b" )); then echo "true"; else echo "false"; fi
true
|
>
|
returns true if argument 1 greater than argument 2
this needs to be escaped with a \ if it occurs inside single brackets; otherwise it can appear between double bracket or between double parentheses |
$ z=10; zz=1000;
$ if [ zz \> z ]; then echo "true"; else echo "false"; fi
true
$ if [[ zz > z ]]; then echo "true"; else echo "false"; fi
true
|
>=
|
returns true if argument 1 greater than or equal to argument 2
this operator needs to go inside double parentheses |
$ a=50; b=10; if (( $a >= $b )); then echo "true"; else echo "false"; fi
true
|
String Operators
| Operator | Meaning | Example |
|---|---|---|
=
|
returns true if two strings are equal | $ a=foo; b=foo; if [[ "$a" = "$b" ]]; then echo "true"; else echo "false"; fi
true
$ a=foo; b=bar; if [[ "$a" = "$b" ]]; then echo "true"; else echo "false"; fi
false
|
==
|
returns true if two strings are equal; equivalent to =
|
$ a=foo; b=foo; if [[ "$a" == "$b" ]]; then echo "true"; else echo "false"; fi
true
$ a=foo; b=bar; if [[ "$a" == "$b" ]]; then echo "true"; else echo "false"; fi
false
Note the difference between quoting a string and not quoting a string: [[ $a == b* ]] # returns true if $a starts with b (pattern matching) [[ $a == "b*" ]] # returns true if $a is literally equal to b* (literal matching) |
!=
|
returns true if strings are not equal | $ a=foo; b=bar; if [[ "$a" != "$b" ]]; then echo "true"; else echo "false"; fi
true
|
<
|
returns true if argument 1 is less than (alphabetically) argument 2; capitals come before non-capitals | $ a=foo; b=bar; if [[ "$a" < "$b" ]]; then echo "true"; else echo "false"; fi
false
$ a=Foo; b=bar; if [[ "$a" < "$b" ]]; then echo "true"; else echo "false"; fi
true
$ a=bar; b=foo; if [[ "$a" < "$b" ]]; then echo "true"; else echo "false"; fi
true
|
>
|
returns true if argument 1 is greater than (alphabetically) argument 2; capitals come before non-capitals | $ a=foo; b=bar; if [[ "$a" > "$b" ]]; then echo "true"; else echo "false"; fi
true
$ a=Foo; b=bar; if [[ "$a" > "$b" ]]; then echo "true"; else echo "false"; fi
false
|
-z
|
returns true if the string is null | $ a=""; b="foobar"; if [ -z "$a" ]; then echo "true"; else echo "false"; fi
true
$ a=""; b="foobar"; if [ -z "$b" ]; then echo "true"; else echo "false"; fi
false
|
-n
|
returns true if the string is NOT null | $ a=""; b="foobar"; if [ -n "$a" ]; then echo "true"; else echo "false"; fi
false
$ a=""; b="foobar"; if [ -n "$b" ]; then echo "true"; else echo "false"; fi
true
|
Compound Operators
You can combine logical operators using and/or operators:
| Operator | Meaning | Example |
|---|---|---|
-a
|
Logical and (both conditions must be true for condition to be true) | if [ "$expr1" -a "$expr2" ]
then
echo "Both expr1 and expr2 are true."
else
echo "Either expr1 or expr2 is false."
fi
|
-o
|
Logical or (either condition must be true for condition to be true) | if [ "$expr1" -o "$expr2" ]
then
echo "Either expr1 or expr2 is true."
else
echo "Both expr1 and expr2 are false."
fi
|
Basic Math
Complex Math
Math Operators
Bash Keyboard Shortcuts
Editing Shortcuts
The following are keyboard shortcuts for easy navigation/editing of commands that are on the Bash command line.
Movement
- C-a - Move to beginning of line
- C-e - Move to end of line
- M-f - Move forward 1 word
- M-b - Move backward 1 word
- C-f - Move forward 1 character
- C-b - Move backward 1 character
Cutting and Pasting
- C-k - kill (cut) from cursor to end of line
- C-w - kill (cut) from cursor to previous whitespace
- C-y - yank (paste) previously killed text at cursor
Uncategorized
- C-L - clear screen, reprint current line at top
- C-u - undo last edit
- C-d - delete character under cursor
References
Floating point math in the shell:
http://www.novell.com/coolsolutions/tools/17043.html
Fast bash math:
http://www.bytemycode.com/snippets/snippet/350/
Bash script iterate through array of values
http://www.tech-recipes.com/rx/636/bash-shell-script-iterate-through-array-values/
| GNU/Linux/Unix the concrete that makes the foundations of the internet.
Compiling Software · Upgrading Software Category:Build Tools · Make · Cmake · Gdb Bash Bash · Bash/Quick (Quick Reference) · Bash Math Text Editors Text Manipulation Command Line Utilities Aptitude · Diff · Make · Patch · Subversion · Xargs Security SSH (Secure Shell) · Gpg (Gnu Privacy Guard) · Category:Security Networking Linux/SSH · Linux/Networking · Linux/File Server Web Servers
|
