Skip to main content
Became Hot Network Question
edited tags
Link
Gilles 'SO- stop being evil'
  • 865.3k
  • 205
  • 1.8k
  • 2.3k
More detail, and clarification
Source Link

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'>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'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

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'

The problem is that yad doesn't accept redirection, so it literally prints the string sample >text 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'

This literally prints the string sample | tee text 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}"

But, I'll be surprised indeed if Linux doesn't already have something like this!

Thank you

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

Source Link

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

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'

The problem is that yad doesn't accept redirection, so it literally prints the string sample >text 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'

This literally prints the string sample | tee text 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}"

But, I'll be surprised indeed if Linux doesn't already have something like this!

Thank you