Bash suppress error

How can I suppress error messages for a shell command? For example, if there are only jpg files in a directory, running ls *.zip gives an error message: $ ls *.zip ls: cannot access '*.zip': No

How can I suppress error messages for a shell command?

For example, if there are only jpg files in a directory, running ls *.zip gives an error message:

   $ ls *.zip
   ls: cannot access '*.zip': No such file or directory

Is there an option to suppress such error messages? I want to use this command in a Bash script, but I want to hide all errors.

Peter Mortensen's user avatar

asked Sep 3, 2015 at 15:31

Peter's user avatar

6

Most Unix commands, including ls, will write regular output to standard output and error messages to standard error, so you can use Bash redirection to throw away the error messages while leaving the regular output in place:

ls *.zip 2> /dev/null

Peter Mortensen's user avatar

answered Sep 3, 2015 at 15:33

AJefferiss's user avatar

AJefferissAJefferiss

1,5731 gold badge12 silver badges17 bronze badges

1

$ ls *.zip 2>/dev/null

will redirect any error messages on stderr to /dev/null (i.e. you won’t see them)

Note the return value (given by $?) will still reflect that an error occurred.

answered Sep 3, 2015 at 15:34

Brian Agnew's user avatar

Brian AgnewBrian Agnew

266k36 gold badges331 silver badges439 bronze badges

To suppress error messages and also return the exit status zero, append || true. For example:

$ ls *.zip && echo hello
ls: cannot access *.zip: No such file or directory
$ ls *.zip 2>/dev/null && echo hello
$ ls *.zip 2>/dev/null || true && echo hello
hello

$ touch x.zip
$ ls *.zip 2>/dev/null || true && echo hello
x.zip
hello

Peter Mortensen's user avatar

answered Nov 11, 2017 at 6:45

A-C's user avatar

A-CA-C

1211 silver badge4 bronze badges

2

I attempted ls -R [existing file] and got an immediate error.
ls: cannot access ‘existing file’: No such file or directory

So, I used the following:

ls -R 2>dev/null | grep -i [existing file]*

ls -R 2>dev/null | grep -i text*

Or, in your case:

ls -R 2>dev/null | grep -i *.zip

answered Jan 4, 2022 at 19:36

Richard's user avatar

1

what I use as solution with raspberry pi3 buster.

ls -R 2>/dev/null | grep -i [existing file]*

answered 2 days ago

fr3d mobile's user avatar

New contributor

fr3d mobile is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

  1. Introduction to Shell and Scripting
  2. Suppress Output of Command in Bash Script

Bash Suppress Output

This article will explain methods to suppress the output of commands using Bash. Moreover, we will also look into redirecting the output to a file for later use.

Introduction to Shell and Scripting

Shell is a program that runs a command line interface in Linux. In Linux, all users can use many different shells.

Commonly, a default GNU’s Bourne-Again Shell (Bash) is used in many Linux distributions.

A file containing a series of commands is called a script. This file is read and executed using the Bash program.

For example, assume a file with the name First.sh has contents as follows:

#!/bin/bash
echo "Hello"
echo "World"

In the above example, First.sh is a script file that contains two echo commands that will display Hello World on the terminal when the script executes. The first line, #!/bin/bash, flags the interpreter’s location to be used for running the script.

We can execute the Bash script on the Linux terminal in two different ways.

Output:

Output:

Suppress Output of Command in Bash Script

We can suppress the output of any commands written in a Bash script during the execution of a Bash script. We can redirect the output to a null device.

For example, if we want to suppress the output of the First.sh script, we can use the following command.

This command suppresses the output of the First.sh script and does not display anything on the terminal. However, if the First.sh script contains any error, it will still be displayed on the terminal.

Consider the following command in the First.sh script.

#!/bin/bash
echo "Hello
echo "World"

In the script, " is missing in the first echo command. The output of the following script run using ./First.sh is as follows:

./First.sh: line 3: unexpected EOF while looking for matching `"'
./First.sh: line 4: syntax error: unexpected end of file

If we run this script using the command ./First.sh > /dev/null will show the same output.

./First.sh: line 3: unexpected EOF while looking for matching `"'
./First.sh: line 4: syntax error: unexpected end of file

Suppress Errors of a Bash Script

If any Bash script contains an error, the ./scriptname > dev/null command does not suppress the error and displays the error on the terminal, as discussed above.

We can suppress the error from the terminal, also. The following commands can suppress the output and the error from the terminal while executing the First.sh Bash script.

./First.sh &>/dev/null
./First.sh >/dev/null 2>&1
./First.sh >/dev/null 2>/dev/null

The first command ./First.sh &>/dev/null maybe not work for all Linux shells.

Record or Store the Output of Bash Scripts

The output is suppressed when we use the /dev/null during the execution of the Bash script. However, we can store or record the output in customized files.

Consider the following First.sh script.

#!/bin/bash
echo "Hello"
echo "World"

We can use the following command to run the First.sh script to store or record the Bash output.

./First.sh > outputfile.out

When we execute the above command, it will not show anything on the terminal; however, a new file with the name outputfile.out is created (if not exists already), and the contents of the file are:

If the outputfile.out already exists, the existing content is varnished, and the script’s new output is stored in the file.

When writing Bash scripts, it’s essential to include error handling to ensure that the script exits gracefully in the event of an error. In this article, we’ll cover all the possible techniques for exiting when errors occur in Bash scripts, including the use of exit codes, the “set -e” option, the “trap” command, and command substitution. In this article, we will discuss the various techniques and methods to handle errors and exit gracefully in Bash scripts. Error handling is an essential aspect of writing scripts, as it helps to ensure that the script exits in an appropriate manner, even when an error occurs. Without proper error handling, a script might continue to execute even when an error has occurred, leading to unexpected results or even system crashes.

We will start by discussing the use of exit codes, which are used to indicate the status of the script’s execution. We will then move on to the “set -e” option, which causes the script to exit immediately if any command exits with a non-zero exit code. Next, we will cover the “trap” command, which allows you to specify a command or set of commands to be executed when the script exits, whether it’s due to an error or not. Then, we will look at command substitution, which allows you to capture the output of a command and assign it to a variable. Finally, we will discuss the technique of suppressing error messages.

Exiting when errors occur in Bash scripts is an important aspect of script development, as it helps to ensure that the script stops running and exits cleanly in the event of an error. There are several ways to exit a Bash script when an error occurs, including using the exit command, using the return command, and using the trap command. It is also important to include error handling and error messages in your script to provide useful information to the user in the event of an error.

Exit Codes

Exit codes are used to indicate the status of the script’s execution, with a zero exit code indicating success and non-zero codes indicating an error. One of the simplest ways to exit a Bash script when an error occurs is to use the “exit” command with a non-zero exit code. For example, the following script exits with a code of 1 if the “mkdir” command fails:

Script:

#!/bin/bash
mkdir /tmp/example
if [ $? -ne 0 ]; then
  exit 1
fi

Output:

Exit codes

Another way to check the exit code of a command is by using the $? variable. For example, the following script also exits with a code of 1 if the “mkdir” command fails:

Script:

#!/bin/bash

mkdir /home/lucifer/yash/first

if [ $? -ne 0 ]; then

  echo “Error: Failed to create directory”

  exit 1

fi

Output:

Exit Codes

Set -e Option

Another way to exit a Bash script when an error occurs is to use the “set -e” option. This option causes the script to exit immediately if any command exits with a non-zero exit code. For example, the following script exits immediately if the “mkdir” command fails:

Script:

#!/bin/bash
set -e
mkdir /tmp/example

Output:

Using -e option

Trap Command

The trap command allows you to specify a command or set of commands to be executed when the script exits, whether it’s due to an error or not. For example, the following script uses the trap command to delete the directory if the script exits with a non-zero exit code:

Script:

#!/bin/bash
trap 'rm -rf /tmp/example' EXIT
mkdir /tmp/example
# some commands here
exit 0

Output:

Using trap command

Command Substitution

Another way to handle errors in Bash scripts is by using command substitution. This technique allows you to capture the output of a command and assign it to a variable. For example, the following script captures the output of the “mkdir” command and assigns it to the “result” variable. If the command fails, the script exits with a code of 1:

Script:

#!/bin/bash
result=$(mkdir /tmp/example)
if [ $? -ne 0 ]; then
  echo "Error: $result"
  exit 1
fi

Output:

Command Substitution

In this example, if the mkdir command fails to create the directory, the error message will be assigned to the “result” variable and displayed on the console.

Suppressing Error Messages

It is also possible to suppress the error message generated by a command by adding 2> /dev/null to the command.

Script:

#!/bin/bash
mkdir /tmp/example 2> /dev/null
if [ $? -ne 0 ]; then
  exit 1
fi

Output:

Suppressing Error Messages

In this example, if the mkdir command fails to create the directory, the error message will not be displayed on the console.

Exit When Any Command Fails

One way to exit a Bash script when any command fails is to use the set -e option. This option causes the script to exit immediately if any command returns a non-zero exit status.
For example, the following script will exit if the ls command fails to list the contents of the specified directory:

Script:

#!/bin/bash
set -e
ls /nonexistent_directory
echo "This line will not be executed"

If the directory /nonexistent_directory does not exist, the script will exit with an error message like this:

Output:

Exiting when command fails

And it will not execute the second line.

Another way to exit if any command fails is to use the || operator after each command, for example:

Script:

#!/bin/bash

ls /nonexistent_directory || exit 1

echo “This line will not be executed”This script will also exit with an error message

Output:

Exiting when command fails

And it will not execute the second line.

You can also check the exit status of the last command with $? variable and use the if condition with the exit command to exit if any command fails.

Script:

#!/bin/bash
ls /nonexistent_directory
if [ $? -ne 0 ]
then
  echo "Command failed"
  exit 1
else
  echo "Command Successful"
fi

This script will also exit with an error message:

Output:

Exiting when command fails

And it will not execute the second line.

It is important to note that, when using the set -e option or || operator, the script will exit even if the failure is in a command inside a function or a subshell. To prevent this behavior, you can use set +e before the function or subshell and set -e after it.

Exit Only When Specific Commands Fail

To exit a Bash script only when specific commands fail, you can use the || operator in conjunction with the exit command. The || operator is used to execute a command only if the preceding command fails (returns a non-zero exit status).
For example, the following script will exit if the ls command fails to list the contents of the specified directory, but will continue to execute subsequent commands even if they fail:

Script:

#!/bin/bash
ls /nonexistent_directory || exit 1
echo "This line will be executed"
rm /nonexistent_file || true
echo "This line will also be executed"

If the directory /nonexistent_directory does not exist, the script will exit with an error message like this:

Output:

Exiting when specific command fails

And it will not execute the second and third lines.

It is important to note that, the true command is used to prevent the rm command from causing the script to exit if it fails. Without the true command, the script would exit if the file /nonexistent_file does not exist.

Another way to achieve this is by using an if statement and checking the exit status of the command with $? variable, like:

Script:

#!/bin/bash
ls /nonexistent_directory
if [ $? -ne 0 ]
then
  echo "Command failed"
  exit 1
else
  echo "Command Successful"
fi
echo "This line will be executed"
rm /nonexistent_file
if [ $? -ne 0 ]
then
  echo "Command failed"
else
  echo "Command Successful"
fi

This script will also exit with an error message:

Output:

Exiting when specific command fails

And it will not execute the second and third lines.

It is important to note that, the exit status of the last executed command is stored in the $? variable. And you can use it to check if the command is successful or not.

Conclusion

There are multiple ways to handle errors and exit gracefully in Bash scripts. By using exit codes, the “set -e” option, the “trap” command, command substitution, and suppressing error messages, you can ensure that your script exits in an appropriate manner and provides appropriate error handling for your script. It’s essential to test your script with different inputs and error conditions to ensure that it is handling errors as expected. It’s also important to include clear and descriptive error messages that indicate the cause of the error and provide guidance on how to resolve it. With these techniques, you can write robust and reliable Bash scripts that can handle errors and exit gracefully.

Do you want to suppress or hide all the output of a Linux Bash Shell Script ? Do you want to redirect all the output to /dev/null ? Are you looking to silence all the output of a Linux Bash Shell Script ? Are you looking to redirect all the error to output ? Do you want to know how to redirect all the output of Bash Shell script to a file ? If yes then you have reached the correct place.

Here I am going to explain all the different methods that can be used to achieve these tasks. You can use all the below explained methods to not just suppress or hide the output of Linux bash shell script from command line but from the crontab as well. So stay focused!!!

How to Suppress or Hide all the Output of a Linux Bash Shell Script

Also Read: 25 Practical and Useful rpm command examples in Linux{cheatsheet}

Method 1: How to Suppress or Hide all the output of a Linux bash shell script

If you are looking to suppress or hide all the output of a bash shell script from Linux command line as well as from the crontab then you can simply redirect all the output to a file known as /dev/null. This file is known as Black Hole which will engulf everything you give without complaining. Hence this is the best place to redirect all the output. In this method we are sending example.sh script output to /dev/null.

[root@localhost ~]# ./example.sh > /dev/null

Method 2: How to Suppress or Hide all the Error messages from a Linux Shell Script 

Using above method you can easily suppress all the output of a Bash Shell Script but what about the script error messages if there is any ? Error messages will not suppressed by above method. For that you need to use another method where you first need to redirect all the error messages to the output and then redirect all the output to /dev/null using earlier explained method. In this method we are sending example.sh script output and its error messages to /dev/null. 2>&1 simply means send the error to the same place output is being sent.

[root@localhost ~]# ./example.sh > /dev/null 2>&1

Method 3: How to Redirect All the output of a Bash Shell Script to a File

If you think output of the Bash Script will be needed later then instead of redirecting all the output of the script to /dev/null you can redirect and save it in a file for later use. You can check more on Bash Guide for Beginners. In this method we are redirecting example.sh script output to file.log.

[root@localhost ~]# ./example.sh > file.log

Method 4: How to Redirect All the error messages of Bash Shell Script to a File

Similarly for the error messages also you can redirect all the error messages to a file instead of redirecting it to /dev/null from where messages cannot be retrieved back. This is really important in the production or critical cases where you are running a bash shell script in crontab and suddenly some error occurred and now you need to debug that error. In those scenarios saving error messages in a file will be much helpful. In this method we are redirecting example.sh script output and its error messages to file.log.

[root@localhost ~]# ./example.sh > file.log 2>&1

Popular Recommendations:-

How to Defragment an XFS Filesystem in Linux(5 Simple and Effective Steps)

How to Install jq(JSON Processor) on RHEL/CentOS 7/8 

How to Install Arpwatch tool on RHEL/CentOS 7/8(Simple and Effective Steps)

How to Install and Configure Squid Proxy Server on RHEL/CentOS 7/8

Python3: ModuleNotFoundError: No Module Named «prettytable» in Linux 

How to List all the Installed Python Modules in Linux{2 Easy Methods}

Solved: ModuleNotFoundError: No Module Named «requests» in Python 3

How to Install and Enable EPEL Repository on RHEL/CentOS 7/8{Simple and Easy Steps}

You can put 2>/dev/null behind a command to suppress errors:

ls /home/cas/thisfolderdoesntexist -> error

ls /home/cas/thisfolderdoesntexist 2>/dev/null -> no output because error is suppressed.

You can also put 2>/dev/null behind a script to run the complete script with errors suppressed:

./script.sh 2>/dev/null

What your doing is redirecting (>) errors (2) to /dev/null. Every piece of data (in this case the output of your command(s)/script) that is redirected to /dev/null will be completely ignored. See it as a trash can for data.


Edit:
2>/dev/null suppresses the output of the command, not the complete pipe. In the example that you gave, you’re supressing errors from the awk command. If the error is comming from the ls command, do the following (this will suppress errors from the ls command):

ls /bootpool 2>/dev/null | grep boot | awk 'NR==1{print $1}'

If the error is comming from the grep command:

ls /bootpool | grep boot 2>/dev/null | awk 'NR==1{print $1}'

I think you get it now.

A good thing to remember:

1 = stdout = normal output of a command

2 = stderr = error output of a command

0 = stdin = input to a command (this isn’t usefull for redirecting, more for logging)


I also improved your script (using shellcheck, you can install it or use their online tool link):

#!/bin/sh
boot=$(find /bootpool/*boot* 2>/dev/null | sed "s|/.*/||")
data=$(find /datapool/*boot* 2>/dev/null | sed "s|/.*/||")
echo "boot"
if [ "$boot" = "boot" ] 
then
        echo "boot"
        pass=$(grep rootpw /bootpool/boot/loader.conf | grep -o '".*"' | sed 's|"||g' | awk 'BEGIN { ORS = " " } { print }')

elif [ "$data" = "boot" ]
then
        pass=$(grep rootpw /datapool/boot/loader.conf | grep -o '".*"' | sed 's|"||g' | awk 'BEGIN { ORS = " " } { print }')

else
        echo "Couldn't find boot in bootpool nor datapool"
        exit
fi

if [ "$pass" = edjos ]
then
        echo "You are at default password. kindly change the password"
        oldpass=$(grep root /etc/master.passwd | awk 'NR==1 { print $1 }' | cut -d ':' -f 2 | sed 's/$/%/g')
        passwd
        newpass=$(grep root /etc/master.passwd | awk 'NR==1 { print $1 }' | cut -d ':' -f 2 | sed 's/$/%/g')
        if [ "$newpass" != "$oldpass" ]
        then              
                if [ "$boot" = "boot" ]
                then 
                        sed -i.bak '/mfsbsd.rootpw/s/edjos//' /bootpool/boot/loader.conf
                        sed -i.bak '/mfsbsd.rootpwhash/d' /bootpool/boot/loader.conf
                        echo "mfsbsd.rootpwhash="$newpass""  >> /bootpool/boot/loader.conf
                        echo "Great! password updated successfully"

                elif [ "$data" = "boot" ]
                then
                        sed -i.bak '/mfsbsd.rootpw/s/edjos//' /datapool/boot/loader.conf
                        sed -i.bak '/mfsbsd.rootpwhash/d' /datapool/boot/loader.conf
                        echo "mfsbsd.rootpwhash="$newpass""  >> /datapool/boot/loader.conf
                        echo "Great! password updated successfully"
                fi
        fi
else
        echo "Great! you are having authorised password"
fi
  1. You were using == but /bin/sh doesn’t make use of that. Only =. When you use /bin/bash, == will actually become usefull. But as you don’t, you need to use =.
  2. I changed the way you set the boot and data variables. The way you did it was inefficient.
  3. When both $boot and $data are empty, the script will catch it instead of letting you continue. This is handy because in your second if statement, when $oldpass and $newpass aren’t equal, it depends on either $boot or $data to contain «boot». But what if they don’t? That’s what the else is for in the first if-statement.
  4. Putting "" around variables. echo $var -> echo "$var"

In the previous post, we talked about the parameters and options in detail. Today, we will talk about something essential in shell scripting, which are Input, Output, and Redirection.

You can display the output from your shell scripts in two ways:

  • Display output on the screen.
  • Send output to a file.

Table of Contents

1

Standard file descriptors

Everything is a file in Linux, and that includes input and output.

Each process can have nine file descriptors opened at the same time. The file descriptors 0, 1, 2 are kept for the bash shell usage.

0              STDIN.

1              STDOUT.

2              STDERR.

You can use the above file descriptors to control input and output.

You need to fully understand these three because they are like the backbones of your shell scripting. So we are going to describe every one of them in detail.

STDIN

STDIN stands for standard input, which is the keyboard by default.

You can replace the STDIN, which is the keyboard and replace it with a file by using the input redirect symbol (<), it sends the data as keyboard typing. No magic!!

When you type the cat command without anything, it accepts input from STDIN. Any line you type, the cat command prints that line to the screen.

STDOUT

The STDOUT stands for the standard output, which is the screen by default.

You can redirect output to a file using the >> symbol.

If we have a file contains data, you can append data to it using this symbol like this:

pwd >> myfile

The output generated by pwd is appended to myfile without deleting the existed content.

Linux shell scripting Input, Output, and Redirection

The following command tries to redirect the output to a file using > symbol.

ls –l xfile > myfile

redirect error

I have no file called xfile on my PC, and that generates an error that is sent to STDERR.

STDERR

The shell sends errors to the screen by default.

If you need to redirect the errors to a log file instead of sending it to the screen, you can redirect errors using the redirection symbol.

Redirecting errors

We can redirect the errors by placing the file descriptor which is 2 before the redirection symbol like this:

ls -l xfile 2>myfile
cat ./myfile

redirect error to file

As you can see, the error now is in the file and nothing on the screen.

Redirecting errors and normal output

To redirect errors and the normal output, you have to precede each with the proper file descriptor like this:

ls –l myfile xfile anotherfile 2> errorcontent 1> correctcontent

redirect error and data

The ls command result is sent to the correctcontent file using the 1> symbol. And error messages were sent to the errorcontent file using the 2> symbol.

You can redirect normal output and errors to the same file using &> symbol like this:

ls –l myfile xfile anotherfile &> content

redirect-all to file

here, we redirect the errors and normal output to a file named content.

There are two ways for output redirection:

  • Temporarily redirection.
  • Permanently redirection.

Temporary redirections

For temporary redirections, you can use the >&2 symbol like this:

#!/bin/bash
echo "Error message" >&2
echo "Normal message"

temp redirection

So if we run it, we will see both lines printed normally because, as we know, errors go to the screen by default.

You can redirect errors to a file like this:

./myscript 2> myfile

redirect error to file

Shell scripting is Awesome! Normal output is sent to the screen, while the echo message which has >&2 symbol sends errors to the file.

Permanent redirections

If you have much data that need to be redirected, you can have a permanent redirection using the exec command like this:

#!/bin/bash
exec 1>outfile
echo "Permanent redirection"
echo "from a shell to a file."
echo "without redirecting every line"

redirect all to file

If we look at the file called outfile, we will see the output of the echo lines.

We redirect the STDOUT at the beginning, what about in the middle of a script like this:

#!/bin/bash
exec 2>myerror
echo "Script Begining ..."
echo "Redirecting Output"
exec 1>myfile
echo "Output goes to the myfile"
echo "Output goes to myerror file" >&2

permanent redirection

The exec command redirects all errors to the file myerror, and we send the standard output to the screen.

We use the statement exec 1>myfile to redirect output to the myfile file, and finally, errors go to the myerror file using >&2 symbol.

Redirecting input

You can redirect input to a file instead of STDIN using exec command like this:

exec 0< myfile

This command tells the shell to take the input from a file called myfile instead of STDIN and here is an example:

#!/bin/bash
exec 0<testfile
total=1
while read line; do
	echo "#$total: $line"
	total=$(($total + 1))
done

redirect input

Shell scripting is easy.

You know how to use the read command to get user input. If you redirect the STDIN to a file, the read command will try to read from STDIN, which points to the file.

Some Linux system administrators use this technique to read the log files for processing, and we will discuss more ways to read the log on the upcoming posts in a professional way.

Creating custom redirection

You know that there are nine file descriptors, you use only 3 of them for input, output, and error.

The remaining six file descriptors are available for use for input and output redirection.

We can use the exec command to assign a file descriptor for output like this:

#!/bin/bash
exec 3>myfile
echo "This line appears on the screen"
echo "This line stored on myfile" >&3
echo "This line appears on the screen"

create redirection

Creating input file descriptors

To redirect input file descriptors do the following:

1-  Save the STDIN to another file descriptor.

2- Redirecting it to a file.

3- Revert STDIN to its original location.

Look at the following code to understand these steps:

#!/bin/bash
exec 7<&0
exec 0<myfile
total=1
while read line; do
	echo "#$total: $line"
	total=$(($total + 1))
done
exec 0<&7
read -p "Finished? " res
case $res in
y) echo "Goodbye" ;;
n) echo "Sorry, this is the end." ;;
esac

create input file descriptor

We saved the STDIN to file descriptor 7, and redirect the STDIN to a file.

The STDIN reverted to its original location after iterating over file lines.

The last read command just to make sure that STDIN is reverted, and you can use the keyboard normally.

Close file descriptors

When the script exits, the file descriptors will close automatically. If you want to close the file descriptor yourself, redirect the file descriptor to this symbol &- it will be closed.

#!/bin/bash
exec 3> myfile
echo "Testing ..." >&3
exec 3>&-
echo "Nothing works" >&3

closing file descriptor

As you can see, it gives an error of bad file descriptor because it is no longer exist.

lsof Command

The lsof command is used to list all the opened files on the system and background processes.

On many Linux systems like Fedora, the lsof command is located under /usr/sbin.

This is some of the important options for lsof command:

-p: for process ID.

-d: for the file descriptor.

You can get the process PID using the $$ variable.

list opened descriptors

We can use the -a to combine the results of -p option and -d option.

Now, testing the command from a script:

#!/bin/bash
exec 4> myfile1
exec 5> myfile2
exec 6< myfile3
lsof -a -p $$ -d 0,1,2,4,5,6

list custom descriptors

The shell script creates the file descriptors 4 and 5 for writing and 6 for reading.

Suppressing command output

Sometimes you don’t want to see any output. We redirect the output to the black hole, which is /dev/null.

For example, we can suppress errors like this:

ls -al badfile anotherfile 2> /dev/null

You can truncate a file without deleting it completely using the same command.

cat /dev/null > myfile

Now you understand the input, output, how to redirect them, how to create your file descriptor, and redirect to it.

I hope you enjoy it. Keep coming back.

Thank you.

Mokhtar Ebrahim

Mokhtar is the founder of LikeGeeks.com. He works as a Linux system administrator since 2010. He is responsible for maintaining, securing, and troubleshooting Linux servers for multiple clients around the world. He loves writing shell and Python scripts to automate his work.

Понравилась статья? Поделить с друзьями:
  • Bash raise error
  • Bash print error
  • Bash pipe error
  • Bash permission denied termux как исправить
  • Bash output error code