Linux/BASH: Difference between revisions

From Wiki
No edit summary
mNo edit summary
Line 16: Line 16:
command | tee -a file.txt      # stdout to terminal and file (append)
command | tee -a file.txt      # stdout to terminal and file (append)
command |& tee -a file.txt    # stdout+stderr to terminal and file (append)
command |& tee -a file.txt    # stdout+stderr to terminal and file (append)
</pre>
</pre>redirecting output<pre>
 
* redirecting output
<pre>
command 2>&1 >/dev/null        # stderr to stdout and stdout to null
command 2>&1 >/dev/null        # stderr to stdout and stdout to null
</pre>
</pre>
== If ==
== If ==
*
*
Line 32: Line 27:
fi
fi
</pre>
</pre>
== For loop ==





Revision as of 15:27, 24 November 2020

Shebang

  • #!/bin/bash

Output & Piping

  • output to file
command > file.txt             # redirect output
command >> file.txt            # append to file

command &> file.txt            # stdout and stderr to file
command &>> file.txt           # stdout and stderr append to file

command 2> file.txt            # stderr to file
command 2>> file.txt           # stderr append to file

command | tee -a file.txt      # stdout to terminal and file (append)
command |& tee -a file.txt     # stdout+stderr to terminal and file (append)

redirecting output

command 2>&1 >/dev/null # stderr to stdout and stdout to null

If

if [ condition ]
  then command1
  else command2
fi

For loop