Redirections
When you run a command. there are 3 file descriptors open by default. 0
which referes to the standard input of that command. 1
refers to standard output. And 2
refers to standard error.
Before the command runs. you can use a special notation to redirect those file descriptors, duplicate them, close them, or make them refer to other files.
Input Redirection
The general format is as follows:
[n]<word
This will cause the file whose name results from the expansion of word
to be opened for reading on file descriptor n
. Or standard input (file descriptor 0
) if n
is not specified.
Example
cat <file.txt
# Or
cat 0<file.txt
# Or
cat 3<file.txt
Output Redirection
The general format is as follows:
[n]>word
This will cause the file whose name results from the expansion of word
to be opened for writing on file descriptor n
. Or standard output (file descriptor 1
) if n
is not specified. If the file does not exist it is created. If it does exist it is truncated to zero size.
Example
echo "Hello World" >file.txt
# Or
echo "Hello World" 1>file.txt
# Or
echo "Hello World" 3>file.txt
Appending Redirected Output
The general format for appending output is as follows:
[n]>>word
Redirection of output in this fashion causes the file whose name results from the expansion of word
to be opened for appending on file descriptor n
, or the standard output (file descriptor 1
) if n is not specified. If the file does not exist it is created.
Example
echo "Hello World" >>file.txt
# Or
echo "Hello World" 1>>file.txt
# Or
echo "Hello World" 3>>file.txt
Redirecting Standard Output and Standard Error
This construct allows both the standard output (file descriptor 1
) and the standard error output (file descriptor 2
) to be redirected to the file whose name is the expansion of word
.
The format is:
&>word
This is semantically equivalent to:
>word 2>&1
Example
echo "Hello World" &>file.txt
# Or
echo "Hello World" 1>file.txt 2>&2
Appending Standard Output and Standard Error
This construct allows both the standard output (file descriptor 1
) and the standard error output (file descriptor 2
) to be appended to the file whose name is the expansion of word
.
The format of appending all output is:
&>>word
This is semantically equivalent to:
>>word 2>&1
Example
echo "Hello World" &>>file.txt
# Or
echo "Hello World" 1>>file.txt 2>&2
Here String
The format is simple as:
[n]<<<word
This notation causes the string resulted from the expansion of word
to be provided as input at the file descriptor n
. Or at the standard input (file descriptor 0
) if n
is not specified.
Example
cat <<<"hello World"
# Or
cat 0<<<"Hello World"
# Or
cat 3<<<"Hello World"
Duplicating file descriptors
Duplication means to make a file descriptor refers to the same file that another file descriptor refers to. For example. Duplicating file descriptor x
on y
means to make the file descriptor x
refers to the same file that file descriptor y
refers to.
The format is:
[n]<&word
This format causes the file descriptor resulted from the expansion of word
to be duplicated on file descriptor n
. Or on standard input (file descriptor 0
) if n
is not specified.
It is possible to use this other format as well:
[n]>&word
The only difference is that when you omit n
. The default is standard output (file descriptor 1
).
Example
In this example, we will duplicate the file descriptor 1
on 2
.
echo foobar 1>file.txt 2>&1
The file descriptor 1
is made to refer to the file file.txt
. then we duplicated the file descriptor 1
on file descriptor 2
. This means that when the command echo
writes to file descriptor 2
. It will write to the file file.txt
. In other words. the file descriptor 2
is now a copy of the file descriptor 1
.
Closing file descriptors
The syntax for closing file descriptors is
[n]<&-
Notice the -
at the end. This notation causes the file descriptor n
to be closed. or standard input (file descriptor 0
) if n
is not specified.
It is possible to use this other format as well:
[n]>&-
The only difference is that when you omit n
. The default is standard output (file descriptor 1
).
Example
echo foobar 1>&-
In this example, we closed the file descriptor 1
. when the command echo
writes to file descriptor 1
. It will fail because the file descriptor is closed.
Moving file descriptors
This notation is a combination of moving and closing file descriptors. In simple words, moving file descriptor x
to y
means to duplicate file descriptor y
to x
. then close the file descriptor y
.
The format is:
[x]<&y-
This means, duplicate the file descriptor y
to x
. then close y
. if x
is not specified, 0
is the default. An alternative syntax is available:
[x]>&y-
The only difference is that when you omit x
. The default is standard output (file descriptor 1
).
Example
In this example, we will move the file descriptor 2
to 1
.
echo foobar 1>&2-
Now, the file descriptor 1
is made a copy of the file descriptor 2
. and the file descriptor 2
was closed. This means. when the command echo
writes to file descriptor 1
. It will write to the same file to which file descriptor 2
was pointing to. and it will fail to write to file descriptor 2
because it was closed.
Open file for reading and writing
The format is:
[n]<>word
This notation causes the file whose name is the expansion of word
to be opened for both reading and writing on file descriptor n
, or on file descriptor 0 if n
is not specified. If the file does not exist, it is created.
Internals of file descriptor management
It is necessary to make something clear about how file descriptors are managed in bunster. bash
users usually struggle when they see a bunster script using a redirection like this:
echo foobar 9999999>file.txt >&9999999
This code will fail in bash
because 9999999
is not a valid file descriptor. This is logical because bash
relies on the kernel to manage file descriptors. Which -- the kernel -- has a strict rules about the format of file descriptors. Also, when you run this code in bash:
cmd 5>file.txt
Bash literally opens the file descriptor 5
. As a result. the command cmd
will inherit that file descriptor and can read/write to it.
Bunster is built differently. The following code is going to work totally fine in bunster :
echo foobar 9999999>file.txt >&9999999
That's because file descriptors are managed by the bunster runtime. And are not real file descriptors. They're just an alias of a file handler. This means that in the above example. The command echo
will not inherit the file descriptor 9999999
because that file descriptor is not open in reality.
Why should I be concerned about file descriptor managment ?
you shouldn't ! there is nothing to worry about regarding the managment of file descriptors. Because the behavior of your script is totally compatible between bash
and bunster
. You don't have to change anything in your bash
scripts to work in bunster. And vice-versa. The only reason why we decided to mention all these information in the documentation is to clarify that while the behavior is similar. The internals are different. and so don't worry when you run ls /proc/self/fd
and you see a different output in bunster than in bash.