these commands are equally valid, both commands add „content“ to the end of file – if file does not exists it will be created.

  • ls -l . /ect > listing.txt #send the output from ls -l to a file called listing.txt, Standard output to the screen is suppressed but errors will display on the screen
  • ls -l . /ect > listing.txt 2>&1 # sent both standard output and error output to the same listing file
  • ls -l . /ect > listing.txt 2>error.txt #Here we separate the output and errors are listed in the error.txt file
  • ls -l . /ect > listing.txt 2>/dev/null #Here we ignore errors sending them to /dev/null

(src)

echo "content" >> file;

>>file echo "content";

echo $?; # returns the error level (1 = error, 0 = success) of the last run command/application/programm/script
0

Bash / ksh and other modern shell on Linux has three file descriptors:

stdin (0)
stdout (1)
stderr (2)

Syntax To redirect all error to file

The syntax is as follows to redirect errors (stderr) as follows:

command 2> error_messages_go_here.txt

command > normal_output_goes_here.txt 2> error_messages_go_here.txt

normal_output and error_messages

go into the same file

command > everything.txt 2>&1

output normal_output and error_messages to file and screen

#!/bin/bash
# My script to do blah ...
{
command1
command2
} 2>&1 | tee script.log

src: https://www.cyberciti.biz/faq/linux-redirect-error-output-to-file/

prevent overwriting files

there are shell options that can do this:

ls > file1

set -o noclobber; # enable "noclobber"
user@debian:~$ set -o
allexport off
braceexpand on
emacs on
errexit off
errtrace off
functrace off
hashall on
histexpand on
history on
ignoreeof off
interactive-comments on
keyword off
monitor on
noclobber on
noexec off
noglob off
nolog off
notify off
nounset off
onecmd off
physical off
pipefail off
posix off
privileged off
verbose off
vi off
xtrace off
user@debian:~$ ls > file1
-bash: file1: cannot overwrite existing file

# BUT! APPENDING STILL WORKS!
ls >> file1

# like this you can force an overwrite, despite the noclobber option being on
ls >| file1

set +o noclobber; # disable "noclobber"

this dictionary says „clobber“ kind of means to „beat someone / something up“.

why one uses + to disable something is also myserious… would have expected the opposite.

special cases:

Others have answered the basic question: what is it?

Now, why is it useful? You can also feed a string to a command’s stdin like this:


<span class="pln">echo </span><span class="str">"$string"</span> <span class="pun">|</span><span class="pln"> command</span>

However in bash, introducing a pipe means the individual commands are run in subshells. Consider this:


<span class="pln">echo </span><span class="str">"hello world"</span> <span class="pun">|</span><span class="pln"> read first second
echo $second $first</span>

The output of the 2nd echo command a single space. Whaaaa? What happened to my variables? Because the read command is in a pipeline, it is run in a subshell. It correctly reads 2 words from its stdin and assigns to the variables. But then the command completes, the subshell exits and the variables are lost.

Sometimes you can work around this with braces:


<span class="pln">echo </span><span class="str">"hello world"</span> <span class="pun">|</span> <span class="pun">{</span><span class="pln">
    read first second
    echo $second $first
</span><span class="pun">}</span>

That’s OK if your need for the values is contained, but you still don’t have those variables in the current shell of your script. To remedy this confusing situation, use a here-string


<span class="pln">read first second </span><span class="pun">&lt;&lt;&lt;</span> <span class="str">"hello world"</span><span class="pln">
echo $second $first</span>

Ah, much better!

Links:

https://www.gnu.org/software/bash/manual/html_node/Redirections.html

admin