Sample Header Ad - 728x90

Is there a command to write text to a file without redirection, pipe or function?

19 votes
4 answers
6210 views
Pipes and redirection are two of the most powerful functions in Linux, and I love them. However, I'm stuck with a situation where I need to write a fixed piece of text to a file without using a pipe, redirection or a function. I'm using Bash in case that makes a difference. First: Why? ----------- I'll explain why, in case there's a simpler solution. I have a background yad notification with some menu entries. In some of the menu entries, I want the notification to write a fixed piece of text to a file. Here's an example of what I mean. yad --notification --command=quit --menu='Example!echo sample >text.txt' The problem is that yad doesn't accept redirection, so it literally prints the string sample >text.txt instead of redirecting. Likewise, the pipe symbol (|) is a separator in yad; but if you change that, yad takes it as a literal character. For example: yad --notification --command=quit --separator='#' --menu='Example!echo sample | tee text.txt' This literally prints the string sample | tee text.txt instead of piping. There's also no point in writing a function for yad to call, because yad runs in its own space and doesn't recognise the function. Hence my question ----------------- Thus, I want a command like echo, cat or printf that takes an output file as an argument rather than a redirect. I have searched for such a command but cannot find it. I can, of course, write my own and put it in the default path: FILENAME="${1}" shift printf '%s\n' "${*}" >"${FILENAME}" and then yad --notification --command=quit --menu='Example!myscript text.txt sample' But, I'll be surprised indeed if Linux doesn't already have something like this! Thank you
Asked by Paddy Landau (346 rep)
Oct 20, 2020, 01:39 PM
Last activity: Jun 7, 2024, 05:28 PM