summaryrefslogtreecommitdiffhomepage
path: root/bash.html.markdown
diff options
context:
space:
mode:
Diffstat (limited to 'bash.html.markdown')
-rw-r--r--bash.html.markdown36
1 files changed, 31 insertions, 5 deletions
diff --git a/bash.html.markdown b/bash.html.markdown
index 211d2944..a62bd167 100644
--- a/bash.html.markdown
+++ b/bash.html.markdown
@@ -11,6 +11,7 @@ contributors:
- ["Rahil Momin", "https://github.com/iamrahil"]
- ["Gregrory Kielian", "https://github.com/gskielian"]
- ["Etan Reisner", "https://github.com/deryni"]
+ - ["Jonathan Wang", "https://github.com/Jonathansw" ]
filename: LearnBash.sh
---
@@ -54,6 +55,13 @@ echo '$Variable'
# its name without $. If you want to use the variable's value, you should use $.
# Note that ' (single quote) won't expand the variables!
+# Parameter expansion ${ }:
+echo ${Variable}
+# 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
+# Below are other modifications that add onto this expansion
+
# String substitution in variables
echo ${Variable/Some/A}
# This will substitute the first occurrence of "Some" with "A"
@@ -68,6 +76,12 @@ echo ${Foo:-"DefaultValueIfFooIsMissingOrEmpty"}
# This works for null (Foo=) and empty string (Foo=""); zero (Foo=0) returns 0.
# Note that it only returns default value and doesn't change variable value.
+# Brace Expansion { }
+# Used to generate arbitrary strings
+echo {1..10}
+echo {a..z}
+# This will output the range from the start value to the end value
+
# Builtin variables:
# There are some useful builtin variables, like
echo "Last program's return value: $?"
@@ -83,7 +97,7 @@ echo Hello, $Name!
# We have the usual if structure:
# use 'man test' for more info about conditionals
-if [ $Name -ne $USER ]
+if [ $Name != $USER ]
then
echo "Your name isn't your username"
else
@@ -91,12 +105,12 @@ else
fi
# NOTE: if $Name is empty, bash sees the above condition as:
-if [ -ne $USER ]
+if [ != $USER ]
# which is invalid syntax
# so the "safe" way to use potentially empty variables in bash is:
-if [ "$Name" -ne $USER ] ...
+if [ "$Name" != $USER ] ...
# which, when $Name is empty, is seen by bash as:
-if [ "" -ne $USER ] ...
+if [ "" != $USER ] ...
# which works as expected
# There is also conditional execution
@@ -130,6 +144,15 @@ ls -l # Lists every file and directory on a separate line
# .txt files in the current directory:
ls -l | grep "\.txt"
+# Since bash works in the context of a current directory, you might want to
+# run your command in some other directory. We have cd for changing location:
+cd ~ # change to home directory
+cd .. # go up one directory
+ # (^^say, from /home/username/Downloads to /home/username)
+cd /home/username/Documents # change to specified directory
+cd ~/Documents/.. # still in home directory..isn't it??
+
+
# You can redirect command input and output (stdin, stdout, and stderr).
# Read from stdin until ^EOF$ and overwrite hello.py with the lines
# between "EOF":
@@ -261,8 +284,11 @@ grep "^foo.*bar$" file.txt
grep -c "^foo.*bar$" file.txt
# if you literally want to search for the string,
# and not the regex, use fgrep (or grep -F)
-fgrep "^foo.*bar$" file.txt
+fgrep "foobar" file.txt
+# trap command allows you to execute a command when a signal is received by your script.
+# Here trap command will execute rm if any one of the three listed signals is received.
+trap "rm $TEMP_FILE; exit" SIGHUP SIGINT SIGTERM
# Read Bash shell builtins documentation with the bash 'help' builtin:
help