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

[cc lang=”bash” escaped=”true” width=”600″]
#!/bin/bash
# My script to do blah …
{
command1
command2
} 2>&1 | tee script.log

[/cc]

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

prevent overwriting files

there are shell options that can do this:

[cc lang=”bash” escaped=”true” width=”600″]
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”
[/cc]

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:

echo "$string" | command

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

echo "hello world" | read first second
echo $second $first

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:

echo "hello world" | {
    read first second
    echo $second $first
}

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

read first second <<< "hello world"
echo $second $first

Ah, much better!

Links:

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

liked this article?

  • only together we can create a truly free world
  • plz support dwaves to keep it up & running!
  • (yes the info on the internet is (mostly) free but beer is still not free (still have to work on that))
  • really really hate advertisement
  • contribute: whenever a solution was found, blog about it for others to find!
  • talk about, recommend & link to this blog and articles
  • thanks to all who contribute!
admin