1
0
mirror of https://github.com/adambard/learnxinyminutes-docs.git synced 2025-08-11 17:24:29 +02:00

Update bash help:

- update examples of array manipulation
- move built-in variables section one level up
- add note in brace expansion
This commit is contained in:
EmilySeville7cfg
2022-08-19 10:10:34 +10:00
parent ed1ca1c70e
commit 242d5da837

View File

@@ -102,39 +102,41 @@ echo "${foo:-"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:
array0=(one two three four five six) array=(one two three four five six)
# Print first element # Print the first element:
echo $array0 # => "one" echo "${array[0]}" # => "one"
# Print first element # Print all elements:
echo ${array0[0]} # => "one" echo "${array[@]}" # => "one two three four five six"
# Print all elements # Print the number of elements:
echo ${array0[@]} # => "one two three four five six" echo "${#array[@]}" # => "6"
# Print number of elements # Print the number of characters in third element
echo ${#array0[@]} # => "6" echo "${#array[2]}" # => "5"
# Print number of characters in third element # Print 2 elements starting from fourth:
echo ${#array0[2]} # => "5" echo "${array[@]:3:2}" # => "four five"
# Print 2 elements starting from fourth # Print all elements each of them on new line.
echo ${array0[@]:3:2} # => "four five" for item in "${array[@]}"; do
# Print all elements. Each of them on new line. echo "$item"
for i in "${array0[@]}"; do
echo "$i"
done done
# Brace Expansion { }
# Used to generate arbitrary strings
echo {1..10} # => 1 2 3 4 5 6 7 8 9 10
echo {a..z} # => a b c d e f g h i j k l m n o p q r s t u v w x y z
# This will output the range from the start value to the end value
# Built-in variables: # Built-in variables:
# There are some useful built-in variables, like # There are some useful built-in variables, like:
echo "Last program's return value: $?" echo "Last program's return value: $?"
echo "Script's PID: $$" echo "Script's PID: $$"
echo "Number of arguments passed to script: $#" echo "Number of arguments passed to script: $#"
echo "All arguments passed to script: $@" echo "All arguments passed to script: $@"
echo "Script's arguments separated into different variables: $1 $2..." echo "Script's arguments separated into different variables: $1 $2..."
# Brace Expansion {...}
# used to generate arbitrary strings:
echo {1..10} # => 1 2 3 4 5 6 7 8 9 10
echo {a..z} # => a b c d e f g h i j k l m n o p q r s t u v w x y z
# This will output the range from the start value to the end value.
# Note that you can't use variables here:
from=1
to=10
echo {$from..$to} # => {$from..$to}
# Now that we know how to echo and use variables, # Now that we know how to echo and use variables,
# let's learn some of the other basics of bash! # let's learn some of the other basics of bash!