From 6ce734eda524ac2f83224f35dfcbc0cc8b86c037 Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Sun, 23 Feb 2025 14:30:32 -0500 Subject: [PATCH 1/7] WIP Concept: Redirections --- concepts/redirections/.meta/config.json | 11 +++++++++++ concepts/redirections/about.md | 3 +++ concepts/redirections/introduction.md | 3 +++ concepts/redirections/links.json | 6 ++++++ config.json | 5 +++++ 5 files changed, 28 insertions(+) create mode 100644 concepts/redirections/.meta/config.json create mode 100644 concepts/redirections/about.md create mode 100644 concepts/redirections/introduction.md create mode 100644 concepts/redirections/links.json diff --git a/concepts/redirections/.meta/config.json b/concepts/redirections/.meta/config.json new file mode 100644 index 00000000..dea0c17a --- /dev/null +++ b/concepts/redirections/.meta/config.json @@ -0,0 +1,11 @@ +{ + "authors": [ + "glennj" + ], + "contributors": [ + "IsaacG", + "kotp" + ], + "blurb": "Redirections manipulate the input and output of a Bash program." +} + diff --git a/concepts/redirections/about.md b/concepts/redirections/about.md new file mode 100644 index 00000000..c47d8314 --- /dev/null +++ b/concepts/redirections/about.md @@ -0,0 +1,3 @@ +# About Redirections + +TODO diff --git a/concepts/redirections/introduction.md b/concepts/redirections/introduction.md new file mode 100644 index 00000000..b353c72b --- /dev/null +++ b/concepts/redirections/introduction.md @@ -0,0 +1,3 @@ +# Introduction to Redirections + +TODO diff --git a/concepts/redirections/links.json b/concepts/redirections/links.json new file mode 100644 index 00000000..3ba0177f --- /dev/null +++ b/concepts/redirections/links.json @@ -0,0 +1,6 @@ +[ + { + "url": "foo", + "description", "bar" + } +] diff --git a/config.json b/config.json index 602a8e7c..f5ed034e 100644 --- a/config.json +++ b/config.json @@ -1289,6 +1289,11 @@ "uuid": "6bfdc749-a40e-44a8-b26b-a402ba53624b", "slug": "more-arrays", "name": "More about Arrays" + }, + { + "uuid": "fb7effaa-9642-4365-8766-6b2876067302", + "slug": "redirections", + "name": "Redirections" } ], "key_features": [ From 5a3a78f8579532a4b9a80e41159955cdf9732c55 Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Tue, 22 Apr 2025 12:29:35 -0400 Subject: [PATCH 2/7] populate the About document --- concepts/redirections/about.md | 161 ++++++++++++++++++++++++++++++- concepts/redirections/links.json | 16 ++- docs/SYLLABUS.md | 1 + 3 files changed, 175 insertions(+), 3 deletions(-) diff --git a/concepts/redirections/about.md b/concepts/redirections/about.md index c47d8314..cda834a9 100644 --- a/concepts/redirections/about.md +++ b/concepts/redirections/about.md @@ -1,3 +1,162 @@ # About Redirections -TODO +In the world of the command-line interface (CLI), programs often interact with three standard data streams: + +1. **Standard Input** (`stdin`): Where a program receives its input (typically from the keyboard). +1. **Standard Output** (`stdout`): Where a program sends its normal output (typically to the terminal screen). +1. **Standard Error** (`stderr`): Where a program sends error messages (typically to the terminal screen). + +Recall that we introduced these in the [Pipelines and Command Lists][pipelines] chapter. + +Shell redirections are powerful mechanisms that allow you to change the default destinations of these streams. Instead of a program reading from the keyboard or writing to the screen, you can redirect input from a file or send output to a file. This provides flexibility and control over how programs interact with data. + +## Key Redirection Operators + +Here are the most common redirection operators and what they do: + +### `>` (Output Redirection) + +* Purpose: Redirects the standard output (stdout) of a command to a file. +* Behavior: + * If the file doesn't exist, it's created. + * If the file exists, its contents are overwritten. +* Example: + ```bash + # Sends the output of 'ls -l' to 'file_listing.txt' + ls -l > file_listing.txt + ``` + +~~~~exercism/caution +Redirections are performed **before** the command is executed. +This means you cannot redirect to the same file you need to read from. + +```bash +date > date.txt # populate the file with the current date +cat date.txt > date.txt # 'date.txt' is now empty! +``` + +In the second command of the above example, the shell +1. first empties the file named in the redirection, then +2. reads zero bytes from the empty file, and writes them to the file. +~~~~ + +### `>>` (Append Output Redirection) + +* Purpose: Appends the standard output (stdout) of a command to a file. +* Behavior: + * If the file doesn't exist, it's created. + * If the file exists, the output is added to the end of the file. +* Example: + ```bash + # Adds "Another line" to the end of the file + echo "Another line" >> file_listing.txt + ``` + +### `<` (Input Redirection) + +* Purpose: Redirects the standard input (stdin) of a command from a file. +* Behavior: The command reads its input from the specified file instead of the keyboard. +* Example: + ```bash + # 'sort' reads _from_ 'unsorted_data.txt' and + # output is redirected _to_ 'sorted_data.txt' + sort < unsorted_data.txt > sorted_data.txt + ``` + +### `2>` (Error Redirection): + +* Purpose: Redirects the standard error (stderr) of a command to a file. +* Behavior: Error messages are sent to the specified file instead of the terminal. +* Example: + ```bash + rm non_existent_file 2> error_log.txt # Error message from 'rm' is sent to 'error_log.txt' + ``` + +You can _append_ standard error to a file with `2>>`. + +### Additional Redirections + +There are also redirections that can merge both `stdin` and `stderr`. See the [manual][manual] for details. + +## `/dev/null` + +`/dev/null` is a special file that discards all data written to it. +It is sometimes referred to as "the bitbucket". +It is useful for suppressing output or errors. + +```bash +some_command > /dev/null # Discards stdout +some_command 2> /dev/null # Discards stderr +``` + +## Combining redirections + +Multiple redirections can be given at once. + +```bash +some_command < input.txt > output.txt 2> error.txt +``` + +Redirections can be copied. + +```bash +# send stderr to the same destination as stdout +some_command > output.txt 2>&1 +``` + +When you see `2>&1`, read that like "redirect stderr to _whatever stdout is **currently** pointing to_". +Bash processes redirections strictly from left to right, which means you can do stuff like: + +```bash +# redirect stdout to 'output1.txt' +# redirect stderr to the same place +# then change stdout to 'output2.txt' +some_command > output1.txt 2>&1 > output2.txt +``` + +~~~~exercism/advanced +## File Descriptors + +In the context of shell programming and operating systems like Linux/Unix, a file descriptor (FD) is a non-negative integer that serves as an identifier or handle for an open file or I/O resource. Think of it like a label or a reference number that the operating system uses to keep track of open files and data streams. + +The numbers you see on the left-hand side of redirections are file descriptors. + +The standard I/O streams are given the first 3 file descriptors: + +* standard input is FD 0 +* standard output is FD 1 +* standard error is FD 2 + +New file descriptors can be set or created with the `exec` command and redirection operators. + +```bash +# create FD 3 as a copy of stdout +exec 3>&1 +# send stdout to a log file +exec > log.txt + +# then redirect both stdout and file descriptor 3 +echo "some debug message" >&3 # displayed in the terminal +some_command 2>&1 # stdout and stderr send to 'log.txt' +``` + +This technique looks quite convoluted, but it can be very useful in situations where your script is being run by some other software package, and standard output is automatically captured by the software. +The `bats` command works like this. +~~~~ + +## In Summary + +Why are redirections useful? + +* Saving Output: Store the results of a command for later review or processing. +* Logging: Capture error messages for debugging. +* Automation: Chain commands together, using the output of one as the input of another. +* Filtering: Discard unwanted output. +* Batch Processing: Process data from files instead of typing it manually. +* Scripting: Redirections are essential for writing robust shell scripts. + + +Shell redirections give you fine-grained control over how programs interact with data, enabling you to build complex workflows and manage information effectively. + +[pipelines]: https://exercism.org/tracks/bash/concepts/pipelines +[manual]: https://www.gnu.org/software/bash/manual/bash.html#Redirections \ No newline at end of file diff --git a/concepts/redirections/links.json b/concepts/redirections/links.json index 3ba0177f..b8be7c8b 100644 --- a/concepts/redirections/links.json +++ b/concepts/redirections/links.json @@ -1,6 +1,18 @@ [ { - "url": "foo", - "description", "bar" + "url": "https://www.gnu.org/software/bash/manual/bash.html#Redirections", + "description": "Redirections in the manual" + }, + { + "url": "https://mywiki.wooledge.org/BashGuide/InputAndOutput#File_Descriptors", + "description": "File Descriptors (Bash Guide)" + }, + { + "url": "https://mywiki.wooledge.org/BashGuide/InputAndOutput#File_Redirection", + "description": "File Redirection (Bash Guide)" + }, + { + "url": "https://mywiki.wooledge.org/BashGuide/InputAndOutput#File_Descriptor_Manipulation", + "description": "File Descriptor Manipulation (Bash Guide)" } ] diff --git a/docs/SYLLABUS.md b/docs/SYLLABUS.md index d2b56188..ec3e761f 100644 --- a/docs/SYLLABUS.md +++ b/docs/SYLLABUS.md @@ -15,6 +15,7 @@ While the learning exercises are still incomplete, most of the concept documenta * [More About Arrays](https://exercism.org/tracks/bash/concepts/more-arrays) * [Pipelines and Command Lists](https://exercism.org/tracks/bash/concepts/pipelines) * [Functions](https://exercism.org/tracks/bash/concepts/functions) + * [Redirections](https://exercism.org/tracks/bash/concepts/redirections) There will be more. Check the "**What's going on with Bash**" section on the [Bash track page](https://exercism.org/tracks/bash) periodically. From 08f8c9a99e3c3b6d2537319254833190f2b720f8 Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Wed, 23 Apr 2025 10:37:30 -0400 Subject: [PATCH 3/7] review suggestions --- concepts/redirections/.meta/config.json | 2 +- concepts/redirections/about.md | 68 ++++++++++++++++--------- concepts/redirections/links.json | 2 +- 3 files changed, 46 insertions(+), 26 deletions(-) diff --git a/concepts/redirections/.meta/config.json b/concepts/redirections/.meta/config.json index dea0c17a..1a615cc4 100644 --- a/concepts/redirections/.meta/config.json +++ b/concepts/redirections/.meta/config.json @@ -6,6 +6,6 @@ "IsaacG", "kotp" ], - "blurb": "Redirections manipulate the input and output of a Bash program." + "blurb": "Redirection manipulates the input and output of programs." } diff --git a/concepts/redirections/about.md b/concepts/redirections/about.md index cda834a9..aa456d88 100644 --- a/concepts/redirections/about.md +++ b/concepts/redirections/about.md @@ -1,6 +1,6 @@ -# About Redirections +# About Redirection -In the world of the command-line interface (CLI), programs often interact with three standard data streams: +In the world of the command-line interface, programs often interact with three standard data streams: 1. **Standard Input** (`stdin`): Where a program receives its input (typically from the keyboard). 1. **Standard Output** (`stdout`): Where a program sends its normal output (typically to the terminal screen). @@ -8,7 +8,15 @@ In the world of the command-line interface (CLI), programs often interact with t Recall that we introduced these in the [Pipelines and Command Lists][pipelines] chapter. -Shell redirections are powerful mechanisms that allow you to change the default destinations of these streams. Instead of a program reading from the keyboard or writing to the screen, you can redirect input from a file or send output to a file. This provides flexibility and control over how programs interact with data. +~~~~exercism/note +Pipelines can be considered a form of redirection: the standard output from one program is sent directly to the standard input of another. +But we've already covered pipelines. +This document is about redirecting to and from files. +~~~~ + +Shell redirection is a powerful mechanism that allows you to change the destination of these streams. +Instead of a program reading from the keyboard or writing to the screen, you can redirect input from a file or send output to a file. +This provides considerable flexibility and control over how programs interact with data. ## Key Redirection Operators @@ -16,18 +24,19 @@ Here are the most common redirection operators and what they do: ### `>` (Output Redirection) -* Purpose: Redirects the standard output (stdout) of a command to a file. +* Purpose: Redirect the standard output (`stdout`) of a command to a file. * Behavior: - * If the file doesn't exist, it's created. + * If the file doesn't exist, it is created. * If the file exists, its contents are overwritten. * Example: + ```bash # Sends the output of 'ls -l' to 'file_listing.txt' ls -l > file_listing.txt ``` ~~~~exercism/caution -Redirections are performed **before** the command is executed. +Redirection is performed **before** the command is executed. This means you cannot redirect to the same file you need to read from. ```bash @@ -35,18 +44,22 @@ date > date.txt # populate the file with the current date cat date.txt > date.txt # 'date.txt' is now empty! ``` -In the second command of the above example, the shell -1. first empties the file named in the redirection, then -2. reads zero bytes from the empty file, and writes them to the file. +In the second command of the above example, + +1. Bash first truncates the file named in the redirection. + This destroys the previous contents, leaving an empty file. +2. Then bash reads the newly empty file. +3. Then bash writes the contents (i.e., nothing) back to the file. ~~~~ ### `>>` (Append Output Redirection) -* Purpose: Appends the standard output (stdout) of a command to a file. +* Purpose: Append the standard output (`stdout`) of a command to a file. * Behavior: - * If the file doesn't exist, it's created. + * If the file doesn't exist, it is created. * If the file exists, the output is added to the end of the file. * Example: + ```bash # Adds "Another line" to the end of the file echo "Another line" >> file_listing.txt @@ -54,33 +67,40 @@ In the second command of the above example, the shell ### `<` (Input Redirection) -* Purpose: Redirects the standard input (stdin) of a command from a file. +* Purpose: Redirect the standard input (`stdin`) of a command from a file. * Behavior: The command reads its input from the specified file instead of the keyboard. * Example: + ```bash - # 'sort' reads _from_ 'unsorted_data.txt' and - # output is redirected _to_ 'sorted_data.txt' - sort < unsorted_data.txt > sorted_data.txt + # 'tr' reads from 'lowercase.txt' and + # output is redirected to 'uppercase.txt' + tr 'a-z' 'A-Z' < lowercase.txt > uppercase.txt ``` ### `2>` (Error Redirection): -* Purpose: Redirects the standard error (stderr) of a command to a file. +* Purpose: Redirect the standard error (`stderr`) of a command to a file. * Behavior: Error messages are sent to the specified file instead of the terminal. * Example: ```bash - rm non_existent_file 2> error_log.txt # Error message from 'rm' is sent to 'error_log.txt' + # Error message from 'rm' is sent to 'error_log.txt' + rm non_existent_file 2> error_log.txt ``` You can _append_ standard error to a file with `2>>`. ### Additional Redirections -There are also redirections that can merge both `stdin` and `stderr`. See the [manual][manual] for details. +There are also redirections that can merge both `stdin` and `stderr`. +See the [manual][manual] for details. ## `/dev/null` -`/dev/null` is a special file that discards all data written to it. +`/dev/null` is a special file that + +* is an empty file when you read from it, and +* discards all data written to it. + It is sometimes referred to as "the bitbucket". It is useful for suppressing output or errors. @@ -97,7 +117,7 @@ Multiple redirections can be given at once. some_command < input.txt > output.txt 2> error.txt ``` -Redirections can be copied. +A redirection can be copied. ```bash # send stderr to the same destination as stdout @@ -105,7 +125,7 @@ some_command > output.txt 2>&1 ``` When you see `2>&1`, read that like "redirect stderr to _whatever stdout is **currently** pointing to_". -Bash processes redirections strictly from left to right, which means you can do stuff like: +Bash processes redirection instructions strictly from left to right, which means you can do: ```bash # redirect stdout to 'output1.txt' @@ -117,7 +137,8 @@ some_command > output1.txt 2>&1 > output2.txt ~~~~exercism/advanced ## File Descriptors -In the context of shell programming and operating systems like Linux/Unix, a file descriptor (FD) is a non-negative integer that serves as an identifier or handle for an open file or I/O resource. Think of it like a label or a reference number that the operating system uses to keep track of open files and data streams. +In the context of shell programming and operating systems like Linux/Unix, a file descriptor (FD) is a non-negative integer that serves as an identifier or handle for an open file or I/O (Input/Output) resource. +Think of it like a label or a reference number that the operating system uses to keep track of open files and data streams. The numbers you see on the left-hand side of redirections are file descriptors. @@ -146,7 +167,7 @@ The `bats` command works like this. ## In Summary -Why are redirections useful? +Why is redirection useful? * Saving Output: Store the results of a command for later review or processing. * Logging: Capture error messages for debugging. @@ -155,7 +176,6 @@ Why are redirections useful? * Batch Processing: Process data from files instead of typing it manually. * Scripting: Redirections are essential for writing robust shell scripts. - Shell redirections give you fine-grained control over how programs interact with data, enabling you to build complex workflows and manage information effectively. [pipelines]: https://exercism.org/tracks/bash/concepts/pipelines diff --git a/concepts/redirections/links.json b/concepts/redirections/links.json index b8be7c8b..0512682e 100644 --- a/concepts/redirections/links.json +++ b/concepts/redirections/links.json @@ -1,7 +1,7 @@ [ { "url": "https://www.gnu.org/software/bash/manual/bash.html#Redirections", - "description": "Redirections in the manual" + "description": "Redirection in the manual" }, { "url": "https://mywiki.wooledge.org/BashGuide/InputAndOutput#File_Descriptors", From 5d343ba5137d553fb03b829651e7beeda8bc1e14 Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Wed, 23 Apr 2025 10:45:24 -0400 Subject: [PATCH 4/7] renamed directory --- concepts/{redirections => redirection}/.meta/config.json | 0 concepts/{redirections => redirection}/about.md | 0 concepts/{redirections => redirection}/introduction.md | 0 concepts/{redirections => redirection}/links.json | 0 config.json | 4 ++-- docs/SYLLABUS.md | 2 +- 6 files changed, 3 insertions(+), 3 deletions(-) rename concepts/{redirections => redirection}/.meta/config.json (100%) rename concepts/{redirections => redirection}/about.md (100%) rename concepts/{redirections => redirection}/introduction.md (100%) rename concepts/{redirections => redirection}/links.json (100%) diff --git a/concepts/redirections/.meta/config.json b/concepts/redirection/.meta/config.json similarity index 100% rename from concepts/redirections/.meta/config.json rename to concepts/redirection/.meta/config.json diff --git a/concepts/redirections/about.md b/concepts/redirection/about.md similarity index 100% rename from concepts/redirections/about.md rename to concepts/redirection/about.md diff --git a/concepts/redirections/introduction.md b/concepts/redirection/introduction.md similarity index 100% rename from concepts/redirections/introduction.md rename to concepts/redirection/introduction.md diff --git a/concepts/redirections/links.json b/concepts/redirection/links.json similarity index 100% rename from concepts/redirections/links.json rename to concepts/redirection/links.json diff --git a/config.json b/config.json index f5ed034e..6887ea8e 100644 --- a/config.json +++ b/config.json @@ -1292,8 +1292,8 @@ }, { "uuid": "fb7effaa-9642-4365-8766-6b2876067302", - "slug": "redirections", - "name": "Redirections" + "slug": "redirection", + "name": "Redirection" } ], "key_features": [ diff --git a/docs/SYLLABUS.md b/docs/SYLLABUS.md index ec3e761f..570c765c 100644 --- a/docs/SYLLABUS.md +++ b/docs/SYLLABUS.md @@ -15,7 +15,7 @@ While the learning exercises are still incomplete, most of the concept documenta * [More About Arrays](https://exercism.org/tracks/bash/concepts/more-arrays) * [Pipelines and Command Lists](https://exercism.org/tracks/bash/concepts/pipelines) * [Functions](https://exercism.org/tracks/bash/concepts/functions) - * [Redirections](https://exercism.org/tracks/bash/concepts/redirections) + * [Redirection](https://exercism.org/tracks/bash/concepts/redirection) There will be more. Check the "**What's going on with Bash**" section on the [Bash track page](https://exercism.org/tracks/bash) periodically. From f778974cabe96d2da42c87a39107cec28e2abacc Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Wed, 23 Apr 2025 16:09:21 -0400 Subject: [PATCH 5/7] Isaac's lastest suggestions --- concepts/redirection/about.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/concepts/redirection/about.md b/concepts/redirection/about.md index aa456d88..9dacb42e 100644 --- a/concepts/redirection/about.md +++ b/concepts/redirection/about.md @@ -10,8 +10,8 @@ Recall that we introduced these in the [Pipelines and Command Lists][pipelines] ~~~~exercism/note Pipelines can be considered a form of redirection: the standard output from one program is sent directly to the standard input of another. -But we've already covered pipelines. -This document is about redirecting to and from files. +We've already covered pipelines. +This concept will cover redirecting to and from files. ~~~~ Shell redirection is a powerful mechanism that allows you to change the destination of these streams. @@ -107,6 +107,10 @@ It is useful for suppressing output or errors. ```bash some_command > /dev/null # Discards stdout some_command 2> /dev/null # Discards stderr + +# Using /dev/null as input will prevent the command +# from reading data from the keyboard +some_command < /dev/null ``` ## Combining redirections @@ -162,7 +166,7 @@ some_command 2>&1 # stdout and stderr send to 'log.txt' ``` This technique looks quite convoluted, but it can be very useful in situations where your script is being run by some other software package, and standard output is automatically captured by the software. -The `bats` command works like this. +The `bats` command, used to run unit tests for this track, works like this: `bats` captures a test's stdout and stderr, but strings sent to FD 3 will be displayed on the terminal. ~~~~ ## In Summary From a7345b6948ce06a886665fda6039fc13623874b1 Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Wed, 23 Apr 2025 16:09:31 -0400 Subject: [PATCH 6/7] copy introduction from about --- concepts/redirection/introduction.md | 187 ++++++++++++++++++++++++++- 1 file changed, 185 insertions(+), 2 deletions(-) diff --git a/concepts/redirection/introduction.md b/concepts/redirection/introduction.md index b353c72b..34ae8cad 100644 --- a/concepts/redirection/introduction.md +++ b/concepts/redirection/introduction.md @@ -1,3 +1,186 @@ -# Introduction to Redirections +# Introduction to Redirection -TODO +In the world of the command-line interface, programs often interact with three standard data streams: + +1. **Standard Input** (`stdin`): Where a program receives its input (typically from the keyboard). +1. **Standard Output** (`stdout`): Where a program sends its normal output (typically to the terminal screen). +1. **Standard Error** (`stderr`): Where a program sends error messages (typically to the terminal screen). + +Recall that we introduced these in the [Pipelines and Command Lists][pipelines] chapter. + +~~~~exercism/note +Pipelines can be considered a form of redirection: the standard output from one program is sent directly to the standard input of another. +We've already covered pipelines. +This concept will cover redirecting to and from files. +~~~~ + +Shell redirection is a powerful mechanism that allows you to change the destination of these streams. +Instead of a program reading from the keyboard or writing to the screen, you can redirect input from a file or send output to a file. +This provides considerable flexibility and control over how programs interact with data. + +## Key Redirection Operators + +Here are the most common redirection operators and what they do: + +### `>` (Output Redirection) + +* Purpose: Redirect the standard output (`stdout`) of a command to a file. +* Behavior: + * If the file doesn't exist, it is created. + * If the file exists, its contents are overwritten. +* Example: + + ```bash + # Sends the output of 'ls -l' to 'file_listing.txt' + ls -l > file_listing.txt + ``` + +~~~~exercism/caution +Redirection is performed **before** the command is executed. +This means you cannot redirect to the same file you need to read from. + +```bash +date > date.txt # populate the file with the current date +cat date.txt > date.txt # 'date.txt' is now empty! +``` + +In the second command of the above example, + +1. Bash first truncates the file named in the redirection. + This destroys the previous contents, leaving an empty file. +2. Then bash reads the newly empty file. +3. Then bash writes the contents (i.e., nothing) back to the file. +~~~~ + +### `>>` (Append Output Redirection) + +* Purpose: Append the standard output (`stdout`) of a command to a file. +* Behavior: + * If the file doesn't exist, it is created. + * If the file exists, the output is added to the end of the file. +* Example: + + ```bash + # Adds "Another line" to the end of the file + echo "Another line" >> file_listing.txt + ``` + +### `<` (Input Redirection) + +* Purpose: Redirect the standard input (`stdin`) of a command from a file. +* Behavior: The command reads its input from the specified file instead of the keyboard. +* Example: + + ```bash + # 'tr' reads from 'lowercase.txt' and + # output is redirected to 'uppercase.txt' + tr 'a-z' 'A-Z' < lowercase.txt > uppercase.txt + ``` + +### `2>` (Error Redirection): + +* Purpose: Redirect the standard error (`stderr`) of a command to a file. +* Behavior: Error messages are sent to the specified file instead of the terminal. +* Example: + ```bash + # Error message from 'rm' is sent to 'error_log.txt' + rm non_existent_file 2> error_log.txt + ``` + +You can _append_ standard error to a file with `2>>`. + +### Additional Redirections + +There are also redirections that can merge both `stdin` and `stderr`. +See the [manual][manual] for details. + +## `/dev/null` + +`/dev/null` is a special file that + +* is an empty file when you read from it, and +* discards all data written to it. + +It is sometimes referred to as "the bitbucket". +It is useful for suppressing output or errors. + +```bash +some_command > /dev/null # Discards stdout +some_command 2> /dev/null # Discards stderr + +# Using /dev/null as input will prevent the command +# from reading data from the keyboard +some_command < /dev/null +``` + +## Combining redirections + +Multiple redirections can be given at once. + +```bash +some_command < input.txt > output.txt 2> error.txt +``` + +A redirection can be copied. + +```bash +# send stderr to the same destination as stdout +some_command > output.txt 2>&1 +``` + +When you see `2>&1`, read that like "redirect stderr to _whatever stdout is **currently** pointing to_". +Bash processes redirection instructions strictly from left to right, which means you can do: + +```bash +# redirect stdout to 'output1.txt' +# redirect stderr to the same place +# then change stdout to 'output2.txt' +some_command > output1.txt 2>&1 > output2.txt +``` + +~~~~exercism/advanced +## File Descriptors + +In the context of shell programming and operating systems like Linux/Unix, a file descriptor (FD) is a non-negative integer that serves as an identifier or handle for an open file or I/O (Input/Output) resource. +Think of it like a label or a reference number that the operating system uses to keep track of open files and data streams. + +The numbers you see on the left-hand side of redirections are file descriptors. + +The standard I/O streams are given the first 3 file descriptors: + +* standard input is FD 0 +* standard output is FD 1 +* standard error is FD 2 + +New file descriptors can be set or created with the `exec` command and redirection operators. + +```bash +# create FD 3 as a copy of stdout +exec 3>&1 +# send stdout to a log file +exec > log.txt + +# then redirect both stdout and file descriptor 3 +echo "some debug message" >&3 # displayed in the terminal +some_command 2>&1 # stdout and stderr send to 'log.txt' +``` + +This technique looks quite convoluted, but it can be very useful in situations where your script is being run by some other software package, and standard output is automatically captured by the software. +The `bats` command, used to run unit tests for this track, works like this: `bats` captures a test's stdout and stderr, but strings sent to FD 3 will be displayed on the terminal. +~~~~ + +## In Summary + +Why is redirection useful? + +* Saving Output: Store the results of a command for later review or processing. +* Logging: Capture error messages for debugging. +* Automation: Chain commands together, using the output of one as the input of another. +* Filtering: Discard unwanted output. +* Batch Processing: Process data from files instead of typing it manually. +* Scripting: Redirections are essential for writing robust shell scripts. + +Shell redirections give you fine-grained control over how programs interact with data, enabling you to build complex workflows and manage information effectively. + +[pipelines]: https://exercism.org/tracks/bash/concepts/pipelines +[manual]: https://www.gnu.org/software/bash/manual/bash.html#Redirections \ No newline at end of file From adf86736704ad508baaaab3af55208b7d18e0205 Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Wed, 23 Apr 2025 16:11:31 -0400 Subject: [PATCH 7/7] kotp's latest suggestion --- concepts/redirection/about.md | 2 +- concepts/redirection/introduction.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/concepts/redirection/about.md b/concepts/redirection/about.md index 9dacb42e..b9fa5248 100644 --- a/concepts/redirection/about.md +++ b/concepts/redirection/about.md @@ -166,7 +166,7 @@ some_command 2>&1 # stdout and stderr send to 'log.txt' ``` This technique looks quite convoluted, but it can be very useful in situations where your script is being run by some other software package, and standard output is automatically captured by the software. -The `bats` command, used to run unit tests for this track, works like this: `bats` captures a test's stdout and stderr, but strings sent to FD 3 will be displayed on the terminal. +The `bats` command that is used to run unit tests for this track works like this: `bats` captures a test's stdout and stderr, but debug messages sent to FD 3 will be displayed on the terminal. ~~~~ ## In Summary diff --git a/concepts/redirection/introduction.md b/concepts/redirection/introduction.md index 34ae8cad..06f71d08 100644 --- a/concepts/redirection/introduction.md +++ b/concepts/redirection/introduction.md @@ -166,7 +166,7 @@ some_command 2>&1 # stdout and stderr send to 'log.txt' ``` This technique looks quite convoluted, but it can be very useful in situations where your script is being run by some other software package, and standard output is automatically captured by the software. -The `bats` command, used to run unit tests for this track, works like this: `bats` captures a test's stdout and stderr, but strings sent to FD 3 will be displayed on the terminal. +The `bats` command that is used to run unit tests for this track works like this: `bats` captures a test's stdout and stderr, but debug messages sent to FD 3 will be displayed on the terminal. ~~~~ ## In Summary