From e47f06a56b4e10e7f0f5a5ba2bc7415009b9b1df Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Wed, 14 Aug 2024 17:07:16 -0400 Subject: [PATCH 1/6] WIP: variables concept --- .../commands-and-arguments/.meta/config.json | 3 +- concepts/commands-and-arguments/about.md | 2 +- concepts/variables/.meta/config.json | 7 ++ concepts/variables/about.md | 0 concepts/variables/introduction.md | 101 ++++++++++++++++++ concepts/variables/links.json | 1 + config.json | 5 + 7 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 concepts/variables/.meta/config.json create mode 100644 concepts/variables/about.md create mode 100644 concepts/variables/introduction.md create mode 100644 concepts/variables/links.json diff --git a/concepts/commands-and-arguments/.meta/config.json b/concepts/commands-and-arguments/.meta/config.json index 369535e3..7801669e 100644 --- a/concepts/commands-and-arguments/.meta/config.json +++ b/concepts/commands-and-arguments/.meta/config.json @@ -3,7 +3,8 @@ "glennj" ], "contributors": [ - "IsaacG" + "IsaacG", + "kotp" ], "blurb": "Types of commands; argument splitting; writing scripts." } diff --git a/concepts/commands-and-arguments/about.md b/concepts/commands-and-arguments/about.md index 5357603d..fe9b4050 100644 --- a/concepts/commands-and-arguments/about.md +++ b/concepts/commands-and-arguments/about.md @@ -1,4 +1,4 @@ -# Introduction +# About ## Shells diff --git a/concepts/variables/.meta/config.json b/concepts/variables/.meta/config.json new file mode 100644 index 00000000..aaa0f126 --- /dev/null +++ b/concepts/variables/.meta/config.json @@ -0,0 +1,7 @@ +{ + "authors": [ + "glennj" + ], + "contributors": [], + "blurb": "Variables and variable expansions." +} diff --git a/concepts/variables/about.md b/concepts/variables/about.md new file mode 100644 index 00000000..e69de29b diff --git a/concepts/variables/introduction.md b/concepts/variables/introduction.md new file mode 100644 index 00000000..61c34ca7 --- /dev/null +++ b/concepts/variables/introduction.md @@ -0,0 +1,101 @@ +# Introduction + +## Variables + +Variables are a place to hold data. +Bash has two kinds of data: strings and arrays. +We'll talk about arrays later. + +You assign a variable like this +```bash +varname="the value" +``` + +~~~~exercism/note +**Important** -- there must be _no spaces_ around the equal sign! + +This is a variable assignment +```bash +x=10 +``` + +This calls the command `x` with two arguments, `=` and `10`! +```bash +x = 10 +``` +~~~~ + +## Variable Names + +A variable name consists of uppercase letters, lowercase letters, numbers and underscore. +It cannot start with a number. + +~~~~exercism/caution +It is best practice to **avoid using ALLCAPS variable names**. +The shell uses this convention for its parameters, and you don't want to accidentally override shell parameters that have special meaning. +For example: [1][1], [2][2], [3][3] + +[1]: https://stackoverflow.com/q/27555060/7552 +[2]: https://stackoverflow.com/q/28310594/7552 +[3]: https://unix.stackexchange.com/q/114596/4667 +~~~~ + +## Parameters + +Variables can also be called "parameters". +The difference is that a variable is assigned by you, the programmer. +A parameter can be a read-only variable that is set by the shell. + +Some important parameters used by or set by bash include: + +* `PATH` -- a colon-separated list of directories used to find external commands. +* `IFS` -- the "internal field separator". We'll see how it's used below. +* `PWD` -- your current working directory. +* `RANDOM` -- a pseudo-random integer between 0 and 32767. +* `BASH_VERSION` -- the version string of the running instance of bash. + +## Parameter Expansion + +You get the value of a parameter with the `$varname` syntax. + +```bash +x=10 +echo "The value of x is $x" +``` + +To prevent the variable name from being confused with surrounding text, you can enclose the variable name in braces. +For example to print the string `10x10` + +```bash +echo "${x}x$x" +# or +echo "${x}x${x}" +``` + +Some [style guides][google-style-guide] recommend using braces in most cases for readability. + +## Positional Parameters + +As we discussed in the [Commands and Arguments][cmds-args] lesson, commands can take arguments. +Bash scripts are no exception. +The arguments to the current instance of bash are called "positional parameters". +They can be retrieved with these special parameters: + +* `$1` -- the first positional parameter +* `$2` ... `$9` -- subsequent parameters +* `${10}` ... -- double digits require braces. +* `$0` -- the name of the current script. + +### Special Positional Parameters + +$@ +$* +IFS + +## Command Substitution + +## More on Parameter Expansion + + +[google-style-guide]: https://google.github.io/styleguide/shellguide.html +[cmds-args]: https://exercism.org/tracks/bash/concepts/commands-and-arguments diff --git a/concepts/variables/links.json b/concepts/variables/links.json new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/concepts/variables/links.json @@ -0,0 +1 @@ +[] diff --git a/config.json b/config.json index ced8abe3..1f692a1e 100644 --- a/config.json +++ b/config.json @@ -1223,6 +1223,11 @@ "uuid": "07e48a7d-7dd0-477c-96e1-23d2558d507d", "slug": "commands-and-arguments", "name": "Commands and Arguments" + }, + { + "uuid": "e29f24ae-3c3b-479a-9333-d733c1ff8fb4", + "slug": "variables", + "name": "Variables" } ], "key_features": [ From ca4b9e81062081a6e5be8097bd81dcbea62c69f6 Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Wed, 14 Aug 2024 17:07:16 -0400 Subject: [PATCH 2/6] variables concept --- concepts/variables/introduction.md | 99 +++++++++++++++++++++++------- 1 file changed, 77 insertions(+), 22 deletions(-) diff --git a/concepts/variables/introduction.md b/concepts/variables/introduction.md index 61c34ca7..ba0d5069 100644 --- a/concepts/variables/introduction.md +++ b/concepts/variables/introduction.md @@ -14,30 +14,13 @@ varname="the value" ~~~~exercism/note **Important** -- there must be _no spaces_ around the equal sign! -This is a variable assignment -```bash -x=10 -``` +When there are spaces, like in this example, -This calls the command `x` with two arguments, `=` and `10`! ```bash x = 10 ``` -~~~~ - -## Variable Names - -A variable name consists of uppercase letters, lowercase letters, numbers and underscore. -It cannot start with a number. - -~~~~exercism/caution -It is best practice to **avoid using ALLCAPS variable names**. -The shell uses this convention for its parameters, and you don't want to accidentally override shell parameters that have special meaning. -For example: [1][1], [2][2], [3][3] -[1]: https://stackoverflow.com/q/27555060/7552 -[2]: https://stackoverflow.com/q/28310594/7552 -[3]: https://unix.stackexchange.com/q/114596/4667 +bash is going to call the **command** `x` with the **two arguments** `=` and `10`! ~~~~ ## Parameters @@ -54,6 +37,21 @@ Some important parameters used by or set by bash include: * `RANDOM` -- a pseudo-random integer between 0 and 32767. * `BASH_VERSION` -- the version string of the running instance of bash. +## Parameter Names + +A variable name consists of uppercase letters, lowercase letters, numbers and underscore. +It cannot start with a number. + +~~~~exercism/caution +It is best practice to **avoid using ALLCAPS variable names**. +The shell uses this convention for its parameters, and you don't want to accidentally override shell parameters that have special meaning. +For example: [1][1], [2][2], [3][3] + +[1]: https://stackoverflow.com/q/27555060/7552 +[2]: https://stackoverflow.com/q/28310594/7552 +[3]: https://unix.stackexchange.com/q/114596/4667 +~~~~ + ## Parameter Expansion You get the value of a parameter with the `$varname` syntax. @@ -88,14 +86,71 @@ They can be retrieved with these special parameters: ### Special Positional Parameters -$@ -$* -IFS +There are some "special" parameters that help you work with the positional parameters. + +* `$#` expands to the _number_ of positional parameters. +* `"$@"` expands to the list of all of the positional parameters, each as a separate word. +* `"$*"` expands to a single string of all the positional parameters joined together by a character. + * The join character is _the first character_ of the `$IFS` variable, which is a space by default. + +The `"$@"` is the safest way to pass positional parameters to another command while keeping them safely quoted. +It is also what you will use to loop over them: + +```bash +for arg in "$@"; do + do_something_with "$arg" +done +``` ## Command Substitution +A very frequent operation you will do in bash scripts is to capture the output of a command and store it in a variable. +To do this, you will use the command substitution syntax, like this: + +```bash +var=$(command) +``` + +For example, to upper-case a string, you can do this: + +```bash +text="Hello world!" +uppercase=$(echo "$text" | tr '[:lower:]' '[:upper:]') +echo "$uppercase" +# => HELLO WORLD! +``` + ## More on Parameter Expansion +Bash has many builtin facilities to manipulate variables and strings so that you don't need to call out to external commands. + +* Uppercasing text: in the previous section I showed how to use `tr` to upper case some text. To do it in bash: + + ```bash + echo "${text^^}" + ``` + +* Provide a default value if the variable is empty. + + ```bash + default="nothing here" + result="" + echo "${result:-$default}" + # => nothing here + ``` + +* Search and replace. + + ```bash + text="Hello world!" + echo "${text//[aeiou]/X}" + # => HXllX wXrld!" + ``` + +There are many other interesting parameter expansions. +Read about them [in the manual][param-exp]. + [google-style-guide]: https://google.github.io/styleguide/shellguide.html [cmds-args]: https://exercism.org/tracks/bash/concepts/commands-and-arguments +[param-exp]: https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion From 3053b4a1ee88a9a4d0f7d1b167943a1844239ffa Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Sun, 18 Aug 2024 11:38:07 -0400 Subject: [PATCH 3/6] review feedback --- concepts/variables/introduction.md | 42 ++++++++++++++++++------------ 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/concepts/variables/introduction.md b/concepts/variables/introduction.md index ba0d5069..d7979e0d 100644 --- a/concepts/variables/introduction.md +++ b/concepts/variables/introduction.md @@ -2,7 +2,7 @@ ## Variables -Variables are a place to hold data. +_Variables_ are a place to hold data. Bash has two kinds of data: strings and arrays. We'll talk about arrays later. @@ -23,28 +23,27 @@ x = 10 bash is going to call the **command** `x` with the **two arguments** `=` and `10`! ~~~~ -## Parameters - -Variables can also be called "parameters". -The difference is that a variable is assigned by you, the programmer. -A parameter can be a read-only variable that is set by the shell. +Variables can also be called _parameters_. +The two terms will be used interchangably in this document. Some important parameters used by or set by bash include: * `PATH` -- a colon-separated list of directories used to find external commands. -* `IFS` -- the "internal field separator". We'll see how it's used below. +* `IFS` -- the "internal field separator". + We'll see how it's used below. * `PWD` -- your current working directory. + It will be the same value as the output of the `pwd` command. * `RANDOM` -- a pseudo-random integer between 0 and 32767. * `BASH_VERSION` -- the version string of the running instance of bash. -## Parameter Names +## Variable Names -A variable name consists of uppercase letters, lowercase letters, numbers and underscore. +A variable name consists of _uppercase letters, lowercase letters, numbers and underscore_. It cannot start with a number. ~~~~exercism/caution It is best practice to **avoid using ALLCAPS variable names**. -The shell uses this convention for its parameters, and you don't want to accidentally override shell parameters that have special meaning. +The shell uses this convention for its parameters, and you don't want to accidentally overwrite shell parameters that have special meaning. For example: [1][1], [2][2], [3][3] [1]: https://stackoverflow.com/q/27555060/7552 @@ -70,13 +69,13 @@ echo "${x}x$x" echo "${x}x${x}" ``` -Some [style guides][google-style-guide] recommend using braces in most cases for readability. +Some [style guides][google-style-guide] recommend using braces in most cases for readability and/or consistency. ## Positional Parameters As we discussed in the [Commands and Arguments][cmds-args] lesson, commands can take arguments. Bash scripts are no exception. -The arguments to the current instance of bash are called "positional parameters". +The arguments to the current instance of bash are called _positional parameters_. They can be retrieved with these special parameters: * `$1` -- the first positional parameter @@ -84,9 +83,12 @@ They can be retrieved with these special parameters: * `${10}` ... -- double digits require braces. * `$0` -- the name of the current script. +Arguments to a shell function are also positional parameters, handled just like this. +We'll see more about functions in a later lesson. + ### Special Positional Parameters -There are some "special" parameters that help you work with the positional parameters. +There are some _special parameters_ that help you work with the positional parameters. * `$#` expands to the _number_ of positional parameters. * `"$@"` expands to the list of all of the positional parameters, each as a separate word. @@ -94,7 +96,7 @@ There are some "special" parameters that help you work with the positional param * The join character is _the first character_ of the `$IFS` variable, which is a space by default. The `"$@"` is the safest way to pass positional parameters to another command while keeping them safely quoted. -It is also what you will use to loop over them: +It is also what you will use to loop over the positional parameters: ```bash for arg in "$@"; do @@ -102,10 +104,12 @@ for arg in "$@"; do done ``` +In practice, `"$@"` is used more often than `"$*"`. + ## Command Substitution A very frequent operation you will do in bash scripts is to capture the output of a command and store it in a variable. -To do this, you will use the command substitution syntax, like this: +To do this, you will use _command substitution_. ```bash var=$(command) @@ -124,12 +128,16 @@ echo "$uppercase" Bash has many builtin facilities to manipulate variables and strings so that you don't need to call out to external commands. -* Uppercasing text: in the previous section I showed how to use `tr` to upper case some text. To do it in bash: +* Uppercasing text. + In the previous section I showed how to use `tr` to upper case some text. + To do it in bash: ```bash echo "${text^^}" ``` + Similarly, `${text,,}` lowercases the value. + * Provide a default value if the variable is empty. ```bash @@ -147,7 +155,7 @@ Bash has many builtin facilities to manipulate variables and strings so that you # => HXllX wXrld!" ``` -There are many other interesting parameter expansions. +There are many other interesting parameter expansions: extract a substring, get the string length, _assign_ a default value, remove text from the start or end of the value, and more. Read about them [in the manual][param-exp]. From 62458bec6c9af74b0a22bd03fdf5d2e352ed0301 Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Sun, 18 Aug 2024 11:47:02 -0400 Subject: [PATCH 4/6] add links --- concepts/commands-and-arguments/links.json | 4 ++-- concepts/variables/links.json | 11 ++++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/concepts/commands-and-arguments/links.json b/concepts/commands-and-arguments/links.json index ef91ec22..f8e46a20 100644 --- a/concepts/commands-and-arguments/links.json +++ b/concepts/commands-and-arguments/links.json @@ -1,10 +1,10 @@ [ { - "description": "Commands and Arguments in the Bash Guide", + "description": "\"Commands and Arguments\" in the BashGuide", "url": "https://mywiki.wooledge.org/BashGuide/CommandsAndArguments" }, { - "description": "Shell Operation in the bash manual", + "description": "\"Shell Operation\" in the bash manual", "url": "https://www.gnu.org/software/bash/manual/bash.html#Shell-Operation" } ] diff --git a/concepts/variables/links.json b/concepts/variables/links.json index fe51488c..56dd277b 100644 --- a/concepts/variables/links.json +++ b/concepts/variables/links.json @@ -1 +1,10 @@ -[] +[ + { + "description": "\"Parameters\" in the BashGuide", + "url": "https://mywiki.wooledge.org/BashGuide/Parameters" + }, + { + "description": "\"Shell Parameters\" in the bash manual", + "url": "https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameters" + } +] From a26c1c50e2582020a4f8c4c8617fadc5faa05395 Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Tue, 20 Aug 2024 19:31:48 -0400 Subject: [PATCH 5/6] feedback for the introduction; add Isaac as a contributor; add the about doc --- concepts/variables/.meta/config.json | 4 +- concepts/variables/about.md | 164 +++++++++++++++++++++++++++ concepts/variables/introduction.md | 2 +- 3 files changed, 168 insertions(+), 2 deletions(-) diff --git a/concepts/variables/.meta/config.json b/concepts/variables/.meta/config.json index aaa0f126..c6619966 100644 --- a/concepts/variables/.meta/config.json +++ b/concepts/variables/.meta/config.json @@ -2,6 +2,8 @@ "authors": [ "glennj" ], - "contributors": [], + "contributors": [ + "IsaacG" + ], "blurb": "Variables and variable expansions." } diff --git a/concepts/variables/about.md b/concepts/variables/about.md index e69de29b..05252373 100644 --- a/concepts/variables/about.md +++ b/concepts/variables/about.md @@ -0,0 +1,164 @@ +# About + +## Variables + +_Variables_ are a place to hold data. +Bash has two kinds of data: strings and arrays. +We'll talk about arrays later. + +You assign a variable like this +```bash +name="value" +``` + +~~~~exercism/note +**Important** -- there must be _no spaces_ around the equal sign! + +When there are spaces, like in this example, + +```bash +x = 10 +``` + +bash is going to call the **command** `x` with the **two arguments** `=` and `10`! +~~~~ + +Variables can also be called _parameters_. +The two terms will be used interchangably in this document. + +Some important parameters used by or set by bash include: + +* `PATH` -- a colon-separated list of directories used to find external commands. +* `IFS` -- the "internal field separator". + We'll see how it's used below. +* `PWD` -- your current working directory. + It will be the same value as the output of the `pwd` command. +* `RANDOM` -- a pseudo-random integer between 0 and 32767. +* `BASH_VERSION` -- the version string of the running instance of bash. + +## Variable Names + +A variable name consists of _uppercase letters, lowercase letters, numbers and underscore_. +It cannot start with a number. + +~~~~exercism/caution +It is best practice to **avoid using ALLCAPS variable names**. +The shell uses this convention for its parameters, and you don't want to accidentally overwrite shell parameters that have special meaning. +For example: [1][1], [2][2], [3][3] + +[1]: https://stackoverflow.com/q/27555060/7552 +[2]: https://stackoverflow.com/q/28310594/7552 +[3]: https://unix.stackexchange.com/q/114596/4667 +~~~~ + +## Parameter Expansion + +You get the value of a parameter with the `$varname` syntax. + +```bash +x=10 +echo "The value of x is $x" +``` + +To prevent the variable name from being confused with surrounding text, you can enclose the variable name in braces. +For example to print the string `10x10` + +```bash +echo "${x}x$x" +# or +echo "${x}x${x}" +``` + +Some [style guides][google-style-guide] recommend using braces in most cases for readability and/or consistency. + +## Positional Parameters + +As we discussed in the [Commands and Arguments][cmds-args] lesson, commands can take arguments. +Bash scripts are no exception. +The arguments to the current instance of bash are called _positional parameters_. +They can be retrieved with these special parameters: + +* `$1` -- the first positional parameter +* `$2` ... `$9` -- subsequent parameters +* `${10}` ... -- double digits require braces. +* `$0` -- the name of the current script. + +Arguments to a shell function are also positional parameters, handled just like this. +We'll see more about functions in a later lesson. + +### Special Positional Parameters + +There are some _special parameters_ that help you work with the positional parameters. + +* `$#` expands to the _number_ of positional parameters. +* `"$@"` expands to the list of all of the positional parameters, each as a separate word. +* `"$*"` expands to a single string of all the positional parameters joined together by a character. + * The join character is _the first character_ of the `$IFS` variable, which is a space by default. + +The `"$@"` is the safest way to pass positional parameters to another command while keeping them safely quoted. +It is also what you will use to loop over the positional parameters: + +```bash +for arg in "$@"; do + do_something_with "$arg" +done +``` + +In practice, `"$@"` is used more often than `"$*"`. + +## Command Substitution + +A very frequent operation you will do in bash scripts is to capture the output of a command and store it in a variable. +To do this, you will use _command substitution_. + +```bash +var=$(command) +``` + +For example, to upper-case a string, you can do this: + +```bash +text="Hello world!" +uppercase=$(echo "$text" | tr '[:lower:]' '[:upper:]') +echo "$uppercase" +# => HELLO WORLD! +``` + +## More on Parameter Expansion + +Bash has many builtin facilities to manipulate variables and strings so that you don't need to call out to external commands. + +* Uppercasing text. + In the previous section I showed how to use `tr` to upper case some text. + To do it in bash: + + ```bash + echo "${text^^}" + ``` + + Similarly, `${text,,}` lowercases the value. + +* Provide a default value if the variable is empty. + + ```bash + default="nothing here" + result="" + echo "${result:-$default}" + # => nothing here + ``` + +* Search and replace. + + ```bash + text="Hello world!" + echo "${text//[aeiou]/X}" + # => HXllX wXrld!" + ``` + +There are many other interesting parameter expansions: extract a substring, get the string length, _assign_ a default value, remove text from the start or end of the value, and more. +Read about them [in the manual][param-exp]. + + +[google-style-guide]: https://google.github.io/styleguide/shellguide.html +[cmds-args]: https://exercism.org/tracks/bash/concepts/commands-and-arguments +[param-exp]: https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion diff --git a/concepts/variables/introduction.md b/concepts/variables/introduction.md index d7979e0d..501e3070 100644 --- a/concepts/variables/introduction.md +++ b/concepts/variables/introduction.md @@ -8,7 +8,7 @@ We'll talk about arrays later. You assign a variable like this ```bash -varname="the value" +name="value" ``` ~~~~exercism/note From 513e8784be48656c238aa3c5d19ba68d10eb604a Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Wed, 21 Aug 2024 20:25:11 -0400 Subject: [PATCH 6/6] Blurbs --- concepts/commands-and-arguments/.meta/config.json | 2 +- concepts/commands-and-arguments/links.json | 2 +- concepts/variables/.meta/config.json | 2 +- concepts/variables/links.json | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/concepts/commands-and-arguments/.meta/config.json b/concepts/commands-and-arguments/.meta/config.json index 7801669e..1dd5452b 100644 --- a/concepts/commands-and-arguments/.meta/config.json +++ b/concepts/commands-and-arguments/.meta/config.json @@ -6,5 +6,5 @@ "IsaacG", "kotp" ], - "blurb": "Types of commands; argument splitting; writing scripts." + "blurb": "Bash scripts are written as sequences of commands. Learn about command and arguments, types of commands, and the essentials about writing scripts." } diff --git a/concepts/commands-and-arguments/links.json b/concepts/commands-and-arguments/links.json index f8e46a20..fddee526 100644 --- a/concepts/commands-and-arguments/links.json +++ b/concepts/commands-and-arguments/links.json @@ -1,6 +1,6 @@ [ { - "description": "\"Commands and Arguments\" in the BashGuide", + "description": "\"Commands and Arguments\" in the Bash Guide", "url": "https://mywiki.wooledge.org/BashGuide/CommandsAndArguments" }, { diff --git a/concepts/variables/.meta/config.json b/concepts/variables/.meta/config.json index c6619966..82bdabd6 100644 --- a/concepts/variables/.meta/config.json +++ b/concepts/variables/.meta/config.json @@ -5,5 +5,5 @@ "contributors": [ "IsaacG" ], - "blurb": "Variables and variable expansions." + "blurb": "Learn about variables in bash, and some of the variable expansions." } diff --git a/concepts/variables/links.json b/concepts/variables/links.json index 56dd277b..d5c4d7e9 100644 --- a/concepts/variables/links.json +++ b/concepts/variables/links.json @@ -1,6 +1,6 @@ [ { - "description": "\"Parameters\" in the BashGuide", + "description": "\"Parameters\" in the Bash Guide", "url": "https://mywiki.wooledge.org/BashGuide/Parameters" }, {