mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
Update bash help:
- use snake case - fix examples - add notes to expansions
This commit is contained in:
parent
c5ca398ad5
commit
524969c42c
@ -18,6 +18,7 @@ contributors:
|
|||||||
- ["Harry Mumford-Turner", "https://github.com/harrymt"]
|
- ["Harry Mumford-Turner", "https://github.com/harrymt"]
|
||||||
- ["Martin Nicholson", "https://github.com/mn113"]
|
- ["Martin Nicholson", "https://github.com/mn113"]
|
||||||
- ["Mark Grimwood", "https://github.com/MarkGrimwood"]
|
- ["Mark Grimwood", "https://github.com/MarkGrimwood"]
|
||||||
|
- ["Emily Grace Seville", "https://github.com/EmilySeville7cfg"]
|
||||||
filename: LearnBash.sh
|
filename: LearnBash.sh
|
||||||
translators:
|
translators:
|
||||||
- ["Dimitri Kokkonis", "https://github.com/kokkonisd"]
|
- ["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.
|
# As you already figured, comments start with #. Shebang is also a comment.
|
||||||
|
|
||||||
# Simple hello world example:
|
# 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:
|
# Each command starts on a new line, or after a semicolon:
|
||||||
echo 'This is the first line'; echo 'This is the second line'
|
echo "This is the first command"; echo "This is the second command"
|
||||||
# => This is the first line
|
# => This is the first command
|
||||||
# => This is the second line
|
# => This is the second command
|
||||||
|
|
||||||
# Declaring a variable looks like this:
|
# Declaring a variable looks like this:
|
||||||
Variable="Some string"
|
variable="Some string"
|
||||||
|
|
||||||
# But not like this:
|
# But not like this:
|
||||||
Variable = "Some string" # => returns error "Variable: command not found"
|
variable = "Some string" # => returns error "variable: command not found"
|
||||||
# Bash will decide that Variable is a command it must execute and give an error
|
# Bash will decide that `variable` is a command it must execute and give an error
|
||||||
# because it can't be found.
|
# because it can't be found.
|
||||||
|
|
||||||
# Nor like this:
|
# Nor like this:
|
||||||
Variable= 'Some string' # => returns error: "Some string: command not found"
|
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
|
# 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
|
# 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'
|
# as a variable assignment valid only for the scope of the "Some string"
|
||||||
# command.)
|
# command.
|
||||||
|
|
||||||
# Using the variable:
|
# Using the variable:
|
||||||
echo $Variable # => Some string
|
echo "$variable" # => Some string
|
||||||
echo "$Variable" # => Some string
|
echo '$variable' # => $variable
|
||||||
echo '$Variable' # => $Variable
|
|
||||||
# When you use the variable itself — assign it, export it, or else — you write
|
# 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 $.
|
# its name without $. If you want to use the variable's value, you should use $.
|
||||||
# Note that ' (single quote) won't expand the variables!
|
# Note that ' (single quote) won't expand the variables!
|
||||||
|
# You can write variable without surrounding quotes but it's not recommended.
|
||||||
|
|
||||||
# Parameter expansion ${ }:
|
# Parameter expansion ${...}:
|
||||||
echo ${Variable} # => Some string
|
echo "${variable}" # => Some string
|
||||||
# This is a simple usage of parameter expansion
|
# This is a simple usage of parameter expansion such as two examples above.
|
||||||
# Parameter Expansion gets a value from a variable.
|
# Parameter expansion gets a value from a variable.
|
||||||
# It "expands" or prints the value
|
# It "expands" or prints the value.
|
||||||
# During the expansion time the value or parameter can be modified
|
# During the expansion time the value or parameter can be modified.
|
||||||
# Below are other modifications that add onto this expansion
|
# Below are other modifications that add onto this expansion.
|
||||||
|
|
||||||
# String substitution in variables
|
# String substitution in variables:
|
||||||
echo ${Variable/Some/A} # => A string
|
echo "${variable/Some/A}" # => A string
|
||||||
# This will substitute the first occurrence of "Some" with "A"
|
# This will substitute the first occurrence of "Some" with "A".
|
||||||
|
|
||||||
# Substring from a variable
|
# Substring from a variable:
|
||||||
Length=7
|
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
|
# This will return only the first 7 characters of the value
|
||||||
echo ${Variable: -5} # => tring
|
echo "${variable: -5}" # => tring
|
||||||
# This will return the last 5 characters (note the space before -5)
|
# This will return the last 5 characters (note the space before -5).
|
||||||
|
# The space before minus is mandatory here.
|
||||||
|
|
||||||
# String length
|
# String length:
|
||||||
echo ${#Variable} # => 11
|
echo "${#variable}" # => 11
|
||||||
|
|
||||||
# Indirect expansion
|
# Indirect expansion:
|
||||||
OtherVariable="Variable"
|
other_variable="variable"
|
||||||
echo ${!OtherVariable} # => Some String
|
echo ${!other_variable} # => Some string
|
||||||
# This will expand the value of OtherVariable
|
# This will expand the value of `other_variable`.
|
||||||
|
|
||||||
# Default value for variable
|
# Default value for variable:
|
||||||
echo ${Foo:-"DefaultValueIfFooIsMissingOrEmpty"}
|
echo "${foo:-"DefaultValueIfFooIsMissingOrEmpty"}"
|
||||||
# => 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.
|
# Note that it only returns default value and doesn't change variable value.
|
||||||
|
|
||||||
# Declare an array with 6 elements
|
# Declare an array with 6 elements
|
||||||
|
Loading…
Reference in New Issue
Block a user