summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDivay Prakash <divayprakash@users.noreply.github.com>2018-10-24 11:35:38 +0530
committerGitHub <noreply@github.com>2018-10-24 11:35:38 +0530
commitff7f2b7d418927a106de1ee9c09a5aae9715bc33 (patch)
treeee84f4560d5efc51b5cc8de893f85e2c738aa020
parentdb3c25c9d120c7adf72b2a9454a62ef26bf7afd9 (diff)
parent4a7d678c2553bc379542a5901177b8bb2730ce65 (diff)
Merge pull request #3043 from mn113/master
[bash/en] Add string length & regex code
-rw-r--r--bash.html.markdown20
1 files changed, 18 insertions, 2 deletions
diff --git a/bash.html.markdown b/bash.html.markdown
index cda877ad..8c40931e 100644
--- a/bash.html.markdown
+++ b/bash.html.markdown
@@ -16,6 +16,7 @@ contributors:
- ["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
---
@@ -74,8 +75,13 @@ echo ${Variable/Some/A} # => A string
# Substring from a variable
Length=7
-echo ${Variable:0:$Length} # => Some st
-# This will return 7 characters of the string, starting from the first char
+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"}
@@ -174,6 +180,16 @@ 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:
+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