summaryrefslogtreecommitdiffhomepage
path: root/bash.html.markdown
diff options
context:
space:
mode:
authorStanley Lim <slim679975@gmail.com>2019-11-21 10:54:24 -0500
committerGitHub <noreply@github.com>2019-11-21 10:54:24 -0500
commit2b1e1cca08eac0d4dc8f685dbe98d80683ca9d3a (patch)
tree460bb7d5cbc1141f8e710e3704f6d03dc25ea193 /bash.html.markdown
parentd4c5ff14cc8a0717f68746b4fe84cfb4efbdecf6 (diff)
parentf1d03b0318a43441bb96bfdaabbd914eaa985879 (diff)
Merge pull request #1 from adambard/master
Merging from master.
Diffstat (limited to 'bash.html.markdown')
-rw-r--r--bash.html.markdown100
1 files changed, 66 insertions, 34 deletions
diff --git a/bash.html.markdown b/bash.html.markdown
index 3f3e49eb..856db706 100644
--- a/bash.html.markdown
+++ b/bash.html.markdown
@@ -11,29 +11,34 @@ 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"]
+ - ["Jonathan Wang", "https://github.com/Jonathansw"]
- ["Leo Rudberg", "https://github.com/LOZORD"]
- ["Betsy Lorton", "https://github.com/schbetsy"]
- ["John Detter", "https://github.com/jdetter"]
- ["Harry Mumford-Turner", "https://github.com/harrymt"]
+ - ["Martin Nicholson", "https://github.com/mn113"]
filename: LearnBash.sh
+translators:
+ - ["Dimitri Kokkonis", "https://github.com/kokkonisd"]
---
-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
-#!/bin/bash
-# First line of the script is shebang which tells the system how to execute
+#!/usr/bin/env bash
+# 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
@@ -46,7 +51,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
@@ -64,8 +69,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
@@ -76,9 +82,14 @@ echo ${Variable/Some/A} # => A string
Length=7
echo ${Variable:0:Length} # => Some st
# This will return only the first 7 characters of the value
+echo ${Variable: -5} # => tring
+# This will return the last 5 characters (note the space before -5)
+
+# String length
+echo ${#Variable} # => 11
# Default value for variable
-echo ${Foo:-"DefaultValueIfFooIsMissingOrEmpty"}
+echo ${Foo:-"DefaultValueIfFooIsMissingOrEmpty"}
# => 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.
@@ -108,8 +119,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: $#"
@@ -121,7 +132,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
@@ -137,7 +148,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"
@@ -174,9 +185,19 @@ then
echo "This will run if $Name is Daniya OR Zach."
fi
-# Redefine command 'ping' as alias to send only 5 packets
+# 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
+ echo "Valid email!"
+fi
+# Note that =~ only works within double [[ ]] square brackets,
+# 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
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
@@ -189,14 +210,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:
@@ -225,13 +246,16 @@ cp -r srcDirectory/ dst/ # recursively copy
# `mv` is also useful for renaming files!
mv s0urc3.txt dst.txt # sorry, l33t hackers...
-# Since bash works in the context of a current directory, you might want to
+# 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 # also goes 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??
+cd - # change to last directory
+# => /home/username/Documents
# Use subshells to work across directories
(echo "First, I'm here: $PWD") && (cd someDir; echo "Then, I'm here: $PWD")
@@ -256,14 +280,22 @@ print("#stderr", file=sys.stderr)
for line in sys.stdin:
print(line, file=sys.stdout)
EOF
+# Variables will be expanded if the first "EOF" is not quoted
-# Run the hello.py Python script with various stdin, stdout, and
+# 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"
@@ -292,11 +324,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.";;
@@ -304,7 +336,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
@@ -325,14 +357,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"
@@ -412,8 +444,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
@@ -422,7 +454,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
@@ -430,12 +462,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