summaryrefslogtreecommitdiffhomepage
path: root/bash.html.markdown
diff options
context:
space:
mode:
authorDivay Prakash <divayprakash@users.noreply.github.com>2018-10-24 11:04:05 +0530
committerGitHub <noreply@github.com>2018-10-24 11:04:05 +0530
commit4a7d678c2553bc379542a5901177b8bb2730ce65 (patch)
treeb2896f9f02b5a752f860a71a36b1e16c566a8ed7 /bash.html.markdown
parente2949649f054ca069e95a05b04d99bccc30ba45d (diff)
parent8f5a67190705c9a3101653901d8f8a7b48eb1775 (diff)
Merge branch 'master' into master
Diffstat (limited to 'bash.html.markdown')
-rw-r--r--bash.html.markdown32
1 files changed, 31 insertions, 1 deletions
diff --git a/bash.html.markdown b/bash.html.markdown
index 8f141673..8c40931e 100644
--- a/bash.html.markdown
+++ b/bash.html.markdown
@@ -26,7 +26,7 @@ Nearly all examples below can be a part of a shell script or executed directly i
[Read more here.](http://www.gnu.org/software/bash/manual/bashref.html)
```bash
-#!/bin/bash
+#!/usr/bin/env bash
# First line of the script is 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.
@@ -89,6 +89,25 @@ 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.
+# Declare an array with 6 elements
+array0=(one two three four five six)
+# Print first element
+echo $array0 # => "one"
+# Print first element
+echo ${array0[0]} # => "one"
+# Print all elements
+echo ${array0[@]} # => "one two three four five six"
+# Print number of elements
+echo ${#array0[@]} # => "6"
+# Print number of characters in third element
+echo ${#array0[2]} # => "5"
+# Print 2 elements starting from forth
+echo ${array0[@]:3:2} # => "four five"
+# Print all elements. Each of them on new line.
+for i in "${array0[@]}"; do
+ echo "$i"
+done
+
# Brace Expansion { }
# Used to generate arbitrary strings
echo {1..10} # => 1 2 3 4 5 6 7 8 9 10
@@ -171,6 +190,13 @@ 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
+alias ping='ping -c 5'
+# Escape alias and use command with this name instead
+\ping 192.168.1.1
+# Print all aliases
+alias -p
+
# Expressions are denoted with the following format:
echo $(( 10 + 5 )) # => 15
@@ -218,10 +244,13 @@ mv s0urc3.txt dst.txt # sorry, l33t hackers...
# 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")
@@ -246,6 +275,7 @@ 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
# stderr redirections: