summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--bash.html.markdown70
1 files changed, 40 insertions, 30 deletions
diff --git a/bash.html.markdown b/bash.html.markdown
index 8c40931e..0385c46d 100644
--- a/bash.html.markdown
+++ b/bash.html.markdown
@@ -20,21 +20,23 @@ contributors:
filename: LearnBash.sh
---
-Bash is a name of the unix shell, which was also distributed as the shell for the GNU operating system and as default shell on Linux and Mac OS X.
-Nearly all examples below can be a part of a shell script or executed directly in the shell.
+Bash is a name of the unix shell, which was also distributed as the shell
+for the GNU operating system and as default shell on Linux and Mac OS X.
+Nearly all examples below can be a part of a shell script
+or executed directly in the shell.
[Read more here.](http://www.gnu.org/software/bash/manual/bashref.html)
```bash
#!/usr/bin/env bash
-# First line of the script is shebang which tells the system how to execute
+# First line of the script is the shebang which tells the system how to execute
# the script: http://en.wikipedia.org/wiki/Shebang_(Unix)
# As you already figured, comments start with #. Shebang is also a comment.
# Simple hello world example:
echo Hello world! # => Hello world!
-# Each command starts on a new line, or after semicolon:
+# Each command starts on a new line, or after a semicolon:
echo 'This is the first line'; echo 'This is the second line'
# => This is the first line
# => This is the second line
@@ -47,7 +49,7 @@ Variable = "Some string" # => returns error "Variable: command not found"
# Bash will decide that Variable is a command it must execute and give an error
# because it can't be found.
-# Or like this:
+# Nor like this:
Variable= 'Some string' # => returns error: "Some string: command not found"
# Bash will decide that 'Some string' is a command it must execute and give an
# error because it can't be found. (In this case the 'Variable=' part is seen
@@ -65,8 +67,9 @@ echo '$Variable' # => $Variable
# Parameter expansion ${ }:
echo ${Variable} # => Some string
# This is a simple usage of parameter expansion
-# Parameter Expansion gets a value from a variable. It "expands" or prints the value
-# During the expansion time the value or parameter are able to be modified
+# Parameter Expansion gets a value from a variable.
+# It "expands" or prints the value
+# During the expansion time the value or parameter can be modified
# Below are other modifications that add onto this expansion
# String substitution in variables
@@ -114,8 +117,8 @@ echo {1..10} # => 1 2 3 4 5 6 7 8 9 10
echo {a..z} # => a b c d e f g h i j k l m n o p q r s t u v w x y z
# This will output the range from the start value to the end value
-# Builtin variables:
-# There are some useful builtin variables, like
+# Built-in variables:
+# There are some useful built-in variables, like
echo "Last program's return value: $?"
echo "Script's PID: $$"
echo "Number of arguments passed to script: $#"
@@ -127,7 +130,7 @@ echo "Script's arguments separated into different variables: $1 $2..."
# Our current directory is available through the command `pwd`.
# `pwd` stands for "print working directory".
-# We can also use the builtin variable `$PWD`.
+# We can also use the built-in variable `$PWD`.
# Observe that the following are equivalent:
echo "I'm in $(pwd)" # execs `pwd` and interpolates output
echo "I'm in $PWD" # interpolates the variable
@@ -143,7 +146,7 @@ read Name # Note that we didn't need to declare a new variable
echo Hello, $Name!
# We have the usual if structure:
-# use 'man test' for more info about conditionals
+# use `man test` for more info about conditionals
if [ $Name != $USER ]
then
echo "Your name isn't your username"
@@ -180,7 +183,7 @@ then
echo "This will run if $Name is Daniya OR Zach."
fi
-# There is also the =~ operator, which tests a string against a Regex pattern:
+# There is also the `=~` operator, which tests a string against a Regex pattern:
Email=me@example.com
if [[ "$Email" =~ [a-z]+@[a-z]{2,}\.(com|net|org) ]]
then
@@ -190,9 +193,9 @@ fi
# which are subtly different from single [ ].
# See http://www.gnu.org/software/bash/manual/bashref.html#Conditional-Constructs for more on this.
-# Redefine command 'ping' as alias to send only 5 packets
+# Redefine command `ping` as alias to send only 5 packets
alias ping='ping -c 5'
-# Escape alias and use command with this name instead
+# Escape the alias and use command with this name instead
\ping 192.168.1.1
# Print all aliases
alias -p
@@ -205,14 +208,14 @@ echo $(( 10 + 5 )) # => 15
# directory with the ls command:
ls # Lists the files and subdirectories contained in the current directory
-# These commands have options that control their execution:
+# This command has options that control its execution:
ls -l # Lists every file and directory on a separate line
ls -t # Sorts the directory contents by last-modified date (descending)
ls -R # Recursively `ls` this directory and all of its subdirectories
# Results of the previous command can be passed to the next command as input.
-# grep command filters the input with provided patterns. That's how we can list
-# .txt files in the current directory:
+# The `grep` command filters the input with provided patterns.
+# That's how we can list .txt files in the current directory:
ls -l | grep "\.txt"
# Use `cat` to print files to stdout:
@@ -280,10 +283,17 @@ EOF
# Run the hello.py Python script with various stdin, stdout, and
# stderr redirections:
python hello.py < "input.in" # pass input.in as input to the script
+
python hello.py > "output.out" # redirect output from the script to output.out
+
python hello.py 2> "error.err" # redirect error output to error.err
-python hello.py > "output-and-error.log" 2>&1 # redirect both output and errors to output-and-error.log
-python hello.py > /dev/null 2>&1 # redirect all output and errors to the black hole, /dev/null, i.e., no output
+
+python hello.py > "output-and-error.log" 2>&1
+# redirect both output and errors to output-and-error.log
+
+python hello.py > /dev/null 2>&1
+# redirect all output and errors to the black hole, /dev/null, i.e., no output
+
# The output error will overwrite the file if it exists,
# if you want to append instead, use ">>":
python hello.py >> "output.out" 2>> "error.err"
@@ -312,11 +322,11 @@ rm -r tempDir/ # recursively delete
# current directory.
echo "There are $(ls | wc -l) items here."
-# The same can be done using backticks `` but they can't be nested - the preferred way
-# is to use $( ).
+# The same can be done using backticks `` but they can't be nested -
+#the preferred way is to use $( ).
echo "There are `ls | wc -l` items here."
-# Bash uses a case statement that works similarly to switch in Java and C++:
+# Bash uses a `case` statement that works similarly to switch in Java and C++:
case "$Variable" in
#List patterns for the conditions you want to meet
0) echo "There is a zero.";;
@@ -324,7 +334,7 @@ case "$Variable" in
*) echo "It is not null.";;
esac
-# for loops iterate for as many arguments given:
+# `for` loops iterate for as many arguments given:
# The contents of $Variable is printed three times.
for Variable in {1..3}
do
@@ -345,14 +355,14 @@ done
# => 3
# They can also be used to act on files..
-# This will run the command 'cat' on file1 and file2
+# This will run the command `cat` on file1 and file2
for Variable in file1 file2
do
cat "$Variable"
done
# ..or the output from a command
-# This will cat the output from ls.
+# This will `cat` the output from `ls`.
for Output in $(ls)
do
cat "$Output"
@@ -432,8 +442,8 @@ grep "^foo.*bar$" file.txt | grep -v "baz"
# and not the regex, use fgrep (or grep -F)
fgrep "foobar" file.txt
-# The trap command allows you to execute a command whenever your script
-# receives a signal. Here, trap will execute `rm` if it receives any of the
+# The `trap` command allows you to execute a command whenever your script
+# receives a signal. Here, `trap` will execute `rm` if it receives any of the
# three listed signals.
trap "rm $TEMP_FILE; exit" SIGHUP SIGINT SIGTERM
@@ -442,7 +452,7 @@ NAME1=$(whoami)
NAME2=$(sudo whoami)
echo "Was $NAME1, then became more powerful $NAME2"
-# Read Bash shell builtins documentation with the bash 'help' builtin:
+# Read Bash shell built-ins documentation with the bash `help` built-in:
help
help help
help for
@@ -450,12 +460,12 @@ help return
help source
help .
-# Read Bash manpage documentation with man
+# Read Bash manpage documentation with `man`
apropos bash
man 1 bash
man bash
-# Read info documentation with info (? for help)
+# Read info documentation with `info` (`?` for help)
apropos info | grep '^info.*('
man info
info info