From d828c706674aaa64ac706954e079def3a9fc914d Mon Sep 17 00:00:00 2001 From: thomasabishop Date: Thu, 26 Jan 2023 16:02:11 +0000 Subject: [PATCH] Autosave: 2023-01-26 16:02:11 --- Programming_Languages/Shell/File_descriptors.md | 2 +- Programming_Languages/Shell/Redirection.md | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Programming_Languages/Shell/File_descriptors.md b/Programming_Languages/Shell/File_descriptors.md index e05a486..ca6c1f1 100644 --- a/Programming_Languages/Shell/File_descriptors.md +++ b/Programming_Languages/Shell/File_descriptors.md @@ -15,6 +15,6 @@ File descriptors are shorthand for `stdin`, `stdout` and `stderr`: | 1 | Standard output | `stdout` | | 2 | Standard error | `stderr` | -They are typically used when you want to redirect the output of the standard/input /output/error somewhere, e.g a file or as input to another program. A classic case is `[some_command] > /dev/null 2>&1` +They are typically used when you want to [redirect](/Programming_Languages/Shell/Redirection.md) the output of the standard/input /output/error somewhere, e.g a file or as input to another program. A classic case is `[some_command] > /dev/null 2>&1` They are all technically "files" which are open and which append themselves to every process that can run in the shell. For any command or program that you run, you will be able to access `0`, `1` and `2` for them. In this way they are similar to variables like [`$0`](/Programming_Languages/Shell/Passing_arguments_to_scripts.md#passing-arguments) and [`$@`](/Programming_Languages/Shell/Passing_arguments_to_scripts.md#passing-arguments). They have a universal meaning within the context of the shell runtime. diff --git a/Programming_Languages/Shell/Redirection.md b/Programming_Languages/Shell/Redirection.md index e784072..a64601e 100644 --- a/Programming_Languages/Shell/Redirection.md +++ b/Programming_Languages/Shell/Redirection.md @@ -8,7 +8,8 @@ tags: # Redirection ## Redirecting outputs -The symbol `>` is called the **redirection operator** because it redirects the output of a command to another location. You most frequently use this when you want to save contents to a file rather than standard output. + +The symbol `>` is called the **redirection operator** because it redirects `stdout` to another program or file. You most frequently use this when you want to save contents to a file rather than standard output. ```bash ls | grep d* > result.txt @@ -18,6 +19,7 @@ ls | grep d* > result.txt It is common practice to combine redirection with the [file descriptors](/Programming_Languages/Shell/File_descriptors.md) to redirect the output of `stdout` and `stderr`. A common case is to [redirect error output to `/dev/null`](/Programming_Languages/Shell/Redirect_to_dev_null.md). +Redirection defaults to interpreting `>` as the redirection of `stdout` (`1`); ## Redirecting inputs @@ -27,7 +29,13 @@ We can also switch the direction of the redirection symbol and pass in a file to sql-processing-program < data.sql ``` -## Appending +We can redirect a string with three carets: + +```bash +program <<< "this is a string input" +``` + +## Appending We use `>>` to append contents on the next available line of a pre-existing file. Continuing on from the example above: