Fish Shell:
Writing a text file
How to:
To write to a text file in Fish, you can use the echo
command combined with redirection operators. There aren’t popular third-party libraries specifically for file writing in Fish, as the shell’s built-in commands are straightforward and efficient for this purpose.
Writing text to a new file or overwriting an existing file:
echo "Hello, Fish Shell!" > output.txt
This command writes “Hello, Fish Shell!” to output.txt
, creating the file if it doesn’t exist or overwriting it if it does.
Appending text to an existing file:
If you want to add text to the end of an existing file without removing its current content, use the append operator >>
:
echo "Adding new line to file." >> output.txt
Writing multiple lines:
You can write multiple lines to a file by using echo with a newline character \n
, or you can chain multiple echo commands together using semicolons:
echo "First Line\nSecond Line" > output.txt
# OR
echo "First Line" > output.txt; echo "Second Line" >> output.txt
Sample output:
To view the contents of output.txt
after running the above commands, use the cat
command:
cat output.txt
First Line
Second Line
Replacing or appending texts as shown manipulates the file content as per your requirements, demonstrating simple yet powerful ways to work with text files in Fish Shell.