Working with the Linux command-line interface can be frustrating at times. While Bash and shell scripting can be powerful, it can also be frustrating when facing unexpected or obscure error messages. In this blog post, I go over the most common error messages you might encounter and examples that might produce these errors.
Frequent Linux Command Errors
Argument list too long
This error message appears when the list of arguments passed to a command exceeds the maximum length allowed by the system. This can happen if you try to delete or list all files in a directory with many files and using a bash wildcard or simply pass too many arguments. Also, it could happen if you forget to put quotes around an argument that contains spaces, causing Bash to interpret it as multiple arguments.
The maximum number of arguments on your system can be estimated using the command getconf ARG_MAX
which provides the maximum length of the buffer of arguments passed to the exec()
call. Though it is not a reliable way to find the maximum number of a command shell number of arguments, see this
in-depth explanation. For example, you can create a large test folder using a
bash for loop or the command-line dd.
Bash loop example (slow):
$> mkdir test
$> for i in {0..$(($(getconf ARG_MAX)/10))}; do touch test/$i; done
$> ls test/*
bash: ls: Argument list too long
dd
example (fastest):
$> mkdir test
$> dd if=/dev/zero of=test/masterfile bs=1 count=$(($(getconf ARG_MAX)/10))
104857+0 records in
104857+0 records out
104857 bytes (105 kB, 102 KiB) copied, 0.267711 s, 392 kB/s
$> split -b 1 test/masterfile test/
$> ls test/*
bash: ls: Argument list too long
You will face a similar issue when trying to count the number of files in a large directory using ls
. A better alternative would be
to count files using the find and wc commands.
Command not found
This error message appears when you try to run a command that either does not exist or cannot be found in your $PATH. This error can also happen if you make a typo in the command name or try to run a script without making it executable first.
$> foobar
foobar: command not found
Cannot open display
This error message appears when a program tries to open a window on your a given display port but cannot connect to it.The first example below doesn’t specify a $DISPLAY
environment variable that prevents the app from starting correctly.
$firefox
Importing existing firefox profiles from /home/nicolas/.mozilla/firefox
Found default profile: fulzrt75.default
Import done in 0.404 s
Error: no DISPLAY environment variable specified
In the following example, the display is not available or does not exist, leading to the cannot open display
error message.
$ firefox
Error: cannot open display: 0:0
This error can also happen if you are trying to run a program with a GUI interface from an SSH session without X11 forwarding enabled, for example:
$> ssh user@example.com firefox
Error: cannot open display: 0:0
To solve this issue, use the -X
ssh option or add ForwardX11 yes
to your ~/.ssh/config
. The server side will also need to allow X11 forwarding using the X11Forwarding yes
config in /etc/ssh/sshd_config
.
Connection refused
This error message appears when you try to connect to a network service not running or listening on the specified port.
$> wget localhost:123
--2023-03-28 07:17:18-- http://localhost:123/
Resolving localhost (localhost)... 127.0.0.1, ::1
Connecting to localhost (localhost)|127.0.0.1|:123... failed: Connection refused.
Connecting to localhost (localhost)|::1|:123... failed: Connection refused.
Directory and Files
Permission denied
This error message appears when you try to access a file or directory that you do not have permission to access. For example, the following user command tries to read from the /etc/shadow
file that is only accessible by the root user, resulting in an error:
$> cat /etc/shadow
cat: /etc/shadow: Permission denied
No such file or directory
This error message appears when you try to access a file or directory that does not exist. For example, the following cat
command line tries to read from a file that does not exist:
$> cat test
cat: test: No such file or directory
Not a directory
This error message appears when you try to access a directory that is not a directory. For example, the following command line tries to change the current path into a file instead of a directory, resulting in an error:
$> touch test
$> cd test
cd: not a directory: test
File exists
This error message appears when you try to create a file or directory that already exists. For example, this will happen if you try to create a file or directory with the same name as an existing one.
$> mkdir test
$> mkdir test
mkdir: cannot create directory βtestβ: File exists
Invalid option
This error message appears when you pass an invalid option to a command. For example, this can happen if you mistype an option or use an option not supported by the command.
$> ls -3
ls: invalid option -- '3'
Try 'ls --help' for more information.
Bad file descriptor
This error message appears when a program tries to use a file descriptor that is not open. For example, the following command line tries to read from a file descriptor that has not been opened, resulting in an error:
$> read -u 3 var
bash: read: 3: invalid file descriptor: Bad file descriptor
Syntax error
The syntax error message is often indicative of a problem with the syntax of a command or script, often due to a typo. The error message will generally have more details like unexpected end of file or invalid arithmetic operator. You may also get a syntax error if you have an incorrect file redirect or if you include parentheses in a command without proper quoting.
Missing filename:
$> echo "some test" >
bash: syntax error near unexpected token `newline'
Missing quotes:
$> echo 'this (works)'
this (works)
$> echo this (fail)
bash: syntax error near unexpected token `('
unexpected end of file
This error message indicates a problem with the syntax of a command or script. It generally happens if you include an unexpected character, such as a newline character in a command. For example, the following script will result in an error:
#!/bin/bash
# script: test.sh
if [ $# -eq 0 ]
then
echo "The variable is zero"
fi^M
Output:
$ ./test.sh
./test.sh: line 6: syntax error: unexpected end of file
Removing the extra ^M
character will solve the problem.
Another example is when you don’t terminate bodies of a single-line bash function with a semicolon:
#!/bin/bash
# script: test.sh
myfunc () { echo "$@"; exit 1 }
if [ $# -eq 0 ]
then
echo "The variable is zero"
fi
Output:
$ ./test.sh
./test.sh: line 68: syntax error: unexpected end of file
Replacing myfunc () { echo "$@"; exit 1 }
by myfunc () { echo "$@"; exit 1; }
would solve the problem, note the semicolon at the end of the exit
command.
invalid arithmetic operator
This error message appears when you use an
invalid arithmetic operator. For example, the following bash expansion tries to perform arithmetic with a float and considers the dot .
an unsupported operator, resulting in an error.
$> echo $((5.3 + 1))
bash: 5.3 + 1: syntax error: invalid arithmetic operator (error token is ".3 + 1")
Frequent Typo Errors
Division by zero
This error message appears when a program tries to divide a number by zero. For example, the following Bash Arithmetic Expansion tries to divide 5 by 0, resulting in an error:
$> echo $((5 / 0))
bash: 5 / 0: division by 0 (error token is "0")
Ambiguous redirect
This error message appears when there is a problem with a redirect in a Bash script. It can happen if you misuse a redirect operator. The following example tries to redirect the echo
output to a non-existing variable, leading to the error.
$> echo "a" > $file_name
bash: $file_name: ambiguous redirect
For this reason, it is important to quote variables. In this example, quoting the variable name would lead to a more meaningful error message by hinting at the non-existent file or incorrect variable.
$> echo "a" > "${file_name}"
bash: : No such file or directory
Bad substitution
This error message appears when you use an incorrect or unsupported syntax for variable substitution. The following examples try to use an invalid variable substitution syntax, resulting in an error:
- Variable name with stray whitespace:
bash -c '${x }'
- Parameter expansion typo, e.g. using a semicolon instead of comma:
bash -c '${x;-}'
- A feature from a later Bash version: e.g. using bash 3.2 on macOS with parameter expansion
bash -c '${x@Q}'
bash-3.2$ ${x@Q}
${x@Q}
bash: ${x@Q}: bad substitution
π ${parameter@operator}
The expansion is either a transformation of the value of parameter or information about parameter itself, depending on the value of operator. Each operator is a single letter:
Q The expansion is a string that is the value of parameter quoted in a format that can be reused as input.
Other Common Error Messages
Permission denied (publickey)
This error message appears when you try to SSH into a remote server, but the server rejects your SSH key. It can happen if the key is not authorized for use on the server or if the key file is not in the correct format.
Segmentation fault
This error message indicates that a program has attempted to access memory that it is not allowed to access. For example, it could happen if a program tries to read or write to a memory address that has not been allocated.
Device not found
This error message appears when you try to access a device that is not available. For example, this could happen if you try to mount a disk that is not connected, or if you try to access a network share that is not available.
Resource temporarily unavailable
This error message appears when a system is temporarily unable to allocate a resource, such as memory or file descriptors. For example, this could happen if the system is under heavy load or a process uses too many resources.
Unable to connect to remote host
This error occurs when a system cannot connect to a remote host. For example, this could happen if the remote host is down, if there is a problem with the network connection, or if the remote host is not listening on the specified port.
Out of memory
This error message indicates that the system has run out of memory and cannot allocate any more memory to your process. It could happen if you run a program that requires a lot of memory or have many programs running simultaneously.
These are just a few examples of the many error messages you might encounter when working with Bash on Linux. Understanding these error messages and how to troubleshoot them is essential to becoming proficient with the Linux command line.