summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorEmilySeville7cfg <EmilySeville7cf@gmail.com>2022-08-19 09:47:37 +1000
committerEmilySeville7cfg <EmilySeville7cf@gmail.com>2022-08-19 09:47:37 +1000
commit524969c42c82e58fe1acf5ebcd9afe67a51142ca (patch)
tree3de539f5f52b5e11fc7e8db3ac7a8694447361f1
parentc5ca398ad54ab19e182eb1967056e47bf5cb08cb (diff)
Update bash help:
- use snake case - fix examples - add notes to expansions
-rw-r--r--bash.html.markdown86
1 files changed, 44 insertions, 42 deletions
diff --git a/bash.html.markdown b/bash.html.markdown
index c9a805ba..4ab800a4 100644
--- a/bash.html.markdown
+++ b/bash.html.markdown
@@ -18,6 +18,7 @@ contributors:
- ["Harry Mumford-Turner", "https://github.com/harrymt"]
- ["Martin Nicholson", "https://github.com/mn113"]
- ["Mark Grimwood", "https://github.com/MarkGrimwood"]
+ - ["Emily Grace Seville", "https://github.com/EmilySeville7cfg"]
filename: LearnBash.sh
translators:
- ["Dimitri Kokkonis", "https://github.com/kokkonisd"]
@@ -37,67 +38,68 @@ or executed directly in the shell.
# As you already figured, comments start with #. Shebang is also a comment.
# Simple hello world example:
-echo Hello world! # => Hello world!
+echo "Hello world!" # => Hello world!
# 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
+echo "This is the first command"; echo "This is the second command"
+# => This is the first command
+# => This is the second command
# Declaring a variable looks like this:
-Variable="Some string"
+variable="Some string"
# But not like this:
-Variable = "Some string" # => returns error "Variable: command not found"
-# Bash will decide that Variable is a command it must execute and give an error
+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.
# 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
-# as a variable assignment valid only for the scope of the 'Some string'
-# command.)
+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
+# as a variable assignment valid only for the scope of the "Some string"
+# command.
# Using the variable:
-echo $Variable # => Some string
-echo "$Variable" # => Some string
-echo '$Variable' # => $Variable
+echo "$variable" # => Some string
+echo '$variable' # => $variable
# When you use the variable itself — assign it, export it, or else — you write
# its name without $. If you want to use the variable's value, you should use $.
# Note that ' (single quote) won't expand the variables!
-
-# 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 can be modified
-# Below are other modifications that add onto this expansion
-
-# String substitution in variables
-echo ${Variable/Some/A} # => A string
-# This will substitute the first occurrence of "Some" with "A"
-
-# Substring from a variable
-Length=7
-echo ${Variable:0:Length} # => Some st
+# You can write variable without surrounding quotes but it's not recommended.
+
+# Parameter expansion ${...}:
+echo "${variable}" # => Some string
+# This is a simple usage of parameter expansion such as two examples above.
+# 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:
+echo "${variable/Some/A}" # => A string
+# This will substitute the first occurrence of "Some" with "A".
+
+# Substring from a variable:
+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)
+echo "${variable: -5}" # => tring
+# This will return the last 5 characters (note the space before -5).
+# The space before minus is mandatory here.
-# String length
-echo ${#Variable} # => 11
+# String length:
+echo "${#variable}" # => 11
-# Indirect expansion
-OtherVariable="Variable"
-echo ${!OtherVariable} # => Some String
-# This will expand the value of OtherVariable
+# Indirect expansion:
+other_variable="variable"
+echo ${!other_variable} # => Some string
+# This will expand the value of `other_variable`.
-# Default value for variable
-echo ${Foo:-"DefaultValueIfFooIsMissingOrEmpty"}
+# Default value for variable:
+echo "${foo:-"DefaultValueIfFooIsMissingOrEmpty"}"
# => DefaultValueIfFooIsMissingOrEmpty
-# This works for null (Foo=) and empty string (Foo=""); zero (Foo=0) returns 0.
+# 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