bash: ./dir: Is a directory (exit status 126)
- tool
- Bash
- exit code
- 126
- versions
- bash 4.x, bash 5.x
- os
- linux, macos
bash: ./build: Is a directory means the word at the start of the command line is a path to a directory. bash cannot execute a directory, so it stops with exit status 126. The same text comes from a redirection to a directory (> logs), and from programs such as cat that expect a file.
Reproduce it
Executing a directory as a command:
mkdir build
./buildOutput · exit 126
bash: line 2: ./build: Is a directory
mkdir logs
echo hello > logsOutput · exit 1
bash: line 2: logs: Is a directory
The first command exits with 126. The redirection fails before the command runs, so the shell reports the error and exits with 1.
Programs that need a file
The text is also the operating system message EISDIR, which tools print when you give them a directory. The prefix tells you who reports it:
mkdir data
cat dataOutput · exit 1
cat: data: Is a directory
Here cat: is the program that complains. grep has its own page for this because it also offers -r; see the related pages.
Fixes
| You wanted to | Use |
|---|---|
| Enter the directory | cd build |
| See what is inside | ls -l build |
| Run a script inside it | ./build/run.sh |
| Write output to a file in it | echo hello > logs/app.log |
| Process every file below it | find data -type f -exec cat {} + |
Check what a variable holds before using it:
mkdir out
target=out
if [ -d "$target" ]; then
echo "$target is a directory, using $target/result.txt"
target="$target/result.txt"
fi
echo ok > "$target"
cat out/result.txtOutput
out is a directory, using out/result.txt ok
Telling 126 from 127
Both numbers describe a command that did not run. 127 means bash could not find it in PATH. 126 means bash found something at that path but cannot execute it: a directory, a file without execute permission, or a file for another architecture. For the permission case see the related page on Permission denied.
Typical causes in scripts
An empty variable can turn "$DIR/$file" into / plus a name, and a missing argument can make "$1" empty so a path points to the current directory. Quote variables, use set -u to stop on unset ones, and print the value when debugging: echo "target=[$target]".
Prevention
Build paths from a base directory plus a file name every time, and test with -d or -f before writing. In Makefiles and CI scripts, create the directory with mkdir -p and write to a file inside it.
FAQ
What does exit status 126 mean?
126 means the command was found but cannot be executed. A directory, a file without the execute bit and an unsupported binary format all give 126; bash 127 means the command was not found at all.
Why does bash dir/ say Is a directory but dir alone says command not found?
A name without a slash is searched in PATH. A name with a slash is taken as a path, so bash sees that it exists and is a directory.
Can I make cd happen automatically?
bash has the autocd shell option (shopt -s autocd) in interactive shells: typing a directory name then changes into it. Do not rely on it in scripts.