luobuda
luobuda

Reputation: 39

bash redirection get err: bad file descriptor and file still open, why?

when I run a simple cmd in bash, I got:

$echo 12312> aaa.txt
-bash: 12312: Bad file descriptor
$lsof aaa.txt
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF    NODE NAME
bash    20647 root    4r   REG    8,1        0 1409118 aaa.txt
$bash --version
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$uname -a
Linux node39 3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

is a bug here? I'm not familiar with the bash source code, but I want to know why. So please give me some tips,best wish to you.

edit: thx to answer my question, but I really want to know why aaa.txt still opened by bash after error happend.

Upvotes: 1

Views: 244

Answers (1)

asio_guy
asio_guy

Reputation: 3767

spaces - bash is very specific about spaces

echo 1234 > output.txt

int the above example 1234 in written to output.txt using redirection operator >

on the other hand if you omit the spaces

echo 1234> output.txt

redirection operator will treat 1234 as "file descriptor number"

Upvotes: 2

Related Questions