Redirections - 3lsy/minishell GitHub Wiki
In one single command it is only possible to have from ZERO to N redirections.
Redirection Symbol | Description | |
---|---|---|
< |
Redirects input. | Needs the file created. |
> |
Redirects output. | Creates the file. |
<< |
Reads input until a line containing the delimiter is seen. | Needs the file created. |
>> |
Redirects output in append mode. | Creates the file. |
If a simple command has multiple redirections, extract blindly EVERY redirection with it's respective filename. The rest is your command.
$ > hello this thing >> another is confusing << yes
# This will result in
## Redirections: (redirection, filename)
[(>, hello), (>>, another), (<<, yes)]
## Command
[this, thing, is, confusing]
! To confirm If there are multiple redirections, only the last one will redirects it seems.
# Example using 'cat' command to read input from a file
cat < input_file.txt
# Example using < without a command and at the beginning of the line (only when file exists)
< input_file.txt
# Example using 'echo' command to write output to a file
echo "Hello, World!" > output_file.txt
# Example using > without a command and at the beginning of the line
> output_file.txt
ls
output_file.txt
# Example using 'cat' command to read input until the delimiter 'EOF' is encountered
cat << EOF
This is line 1.
This is line 2.
EOF
# Example using << without a command and at the beginning of the line
<< EOF
This is line 1.
This is line 2.
EOF
# Example using 'echo' command to append output to a file
echo "This will be appended." >> output_file.txt
# Example using >> without a command and at the beginning of the line
>> output_file.txt
ls
output_file.txt
# Redirect input from input_file.txt, and redirect output to output_file.txt
cat < input_file.txt > output_file.txt
# Redirect input from input_file.txt, and append output to output_file.txt
cat < input_file.txt >> output_file.txt
# Read input from a here document and write output to output_file.txt
cat << EOF > output_file.txt
This is line 1.
This is line 2.
EOF
# Append input from a here document to output_file.txt
cat >> output_file.txt << EOF
This is line 1.
This is line 2.
EOF