From f39df1da097f8718b375127ad04f2114c5c4e48d Mon Sep 17 00:00:00 2001 From: Martin Nicholson Date: Tue, 16 Jan 2018 15:27:30 +0000 Subject: Added second substring example --- bash.html.markdown | 3 +++ 1 file changed, 3 insertions(+) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 0c097c27..ed4fa54f 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 --- @@ -76,6 +77,8 @@ 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: -Length} +# This will return the last 7 characters (note the space before -Length) # Default value for variable echo ${Foo:-"DefaultValueIfFooIsMissingOrEmpty"} -- cgit v1.2.3 From 462f94b967d4814e57bb6836cfd9d13abd8f9ab7 Mon Sep 17 00:00:00 2001 From: Martin Nicholson Date: Tue, 16 Jan 2018 15:31:32 +0000 Subject: Added string length example --- bash.html.markdown | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index ed4fa54f..fa3c5ebe 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -77,8 +77,11 @@ 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: -Length} -# This will return the last 7 characters (note the space before -Length) +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"} -- cgit v1.2.3 From 8d2055483e8c0a8147414f4695a7cf3e5ede819e Mon Sep 17 00:00:00 2001 From: Martin Nicholson Date: Tue, 16 Jan 2018 16:03:13 +0000 Subject: Added section for =~ operator --- bash.html.markdown | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index fa3c5ebe..354042ec 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -161,6 +161,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 the appropriate [manual section](http://www.gnu.org/software/bash/manual/bashref.html#Conditional-Constructs) for more on this. + # Expressions are denoted with the following format: echo $(( 10 + 5 )) # => 15 -- cgit v1.2.3 From e2949649f054ca069e95a05b04d99bccc30ba45d Mon Sep 17 00:00:00 2001 From: Martin Nicholson Date: Tue, 16 Jan 2018 16:09:49 +0000 Subject: Un-markdown-ify link --- bash.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 354042ec..8f141673 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -169,7 +169,7 @@ then fi # Note that =~ only works within double [[ ]] square brackets, # which are subtly different from single [ ]. -# See the appropriate [manual section](http://www.gnu.org/software/bash/manual/bashref.html#Conditional-Constructs) for more on this. +# See http://www.gnu.org/software/bash/manual/bashref.html#Conditional-Constructs for more on this. # Expressions are denoted with the following format: echo $(( 10 + 5 )) # => 15 -- cgit v1.2.3