From 2dda26010ab52ecd5d54c6fa1b8701288aaaac2d Mon Sep 17 00:00:00 2001 From: Neinei0k Date: Tue, 14 Nov 2017 19:30:57 +0000 Subject: [bash/en] Add arrays and alias --- bash.html.markdown | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 0c097c27..3f3e49eb 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -83,6 +83,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 @@ -155,6 +174,13 @@ then echo "This will run if $Name is Daniya OR Zach." fi +# 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 -- cgit v1.2.3 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 From dbb858281a8c5948606ad3a07225915954a03953 Mon Sep 17 00:00:00 2001 From: kfrncs Date: Fri, 20 Jul 2018 14:38:59 -0400 Subject: missing $ on line 77 - $Length MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Maybe just a zsh quirk? ➜ ~ echo ${Variable:0:Length} zsh: unrecognized modifier --- 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 3f3e49eb..d1e6bf25 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -74,7 +74,7 @@ echo ${Variable/Some/A} # => A string # Substring from a variable Length=7 -echo ${Variable:0:Length} # => Some st +echo ${Variable:0:$Length} # => Some st # This will return only the first 7 characters of the value # Default value for variable -- cgit v1.2.3 From 6f0aec01c577e44b1ea3260f786ef3527eebf204 Mon Sep 17 00:00:00 2001 From: Aidas Bendoraitis Date: Wed, 25 Jul 2018 23:58:31 +0200 Subject: shebang updated --- 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 3f3e49eb..f2a72fdb 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -25,7 +25,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. -- cgit v1.2.3 From 088111396a0dff9a5dff7837e1ae74aa9314b620 Mon Sep 17 00:00:00 2001 From: Tucker Boniface Date: Tue, 2 Oct 2018 19:19:18 -0700 Subject: [bash/en] clarify quoted delimiter for heredocs (fixes #3061) --- bash.html.markdown | 1 + 1 file changed, 1 insertion(+) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index cb805da7..1f59636e 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -256,6 +256,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: -- cgit v1.2.3 From 843626f3d30a06cc1c8b342c155a5dc8af24fbd1 Mon Sep 17 00:00:00 2001 From: Tucker Boniface Date: Tue, 2 Oct 2018 19:23:45 -0700 Subject: [bash/en] add `cd` and `cd -` uses --- 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 cb805da7..e609260f 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -228,10 +228,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") -- cgit v1.2.3 From 9152fe00e54b51ddebaeed9929a3027547cb16a5 Mon Sep 17 00:00:00 2001 From: Tucker Boniface Date: Tue, 2 Oct 2018 19:22:56 -0700 Subject: [bash/en] clarify range of substring --- 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 cb805da7..be879538 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -75,7 +75,7 @@ echo ${Variable/Some/A} # => A string # Substring from a variable Length=7 echo ${Variable:0:$Length} # => Some st -# This will return only the first 7 characters of the value +# This will return 7 characters of the string, starting from the first char # Default value for variable echo ${Foo:-"DefaultValueIfFooIsMissingOrEmpty"} -- cgit v1.2.3