reitermarkus reitermarkus - 4 months ago 10
Bash Question

Terminal shell: How to use echo instead of a text file as command-line arguments

I have a command line utility which needs a text file (to format the output) as argument. I only need the pure value and no formatting, so I'd like to have a one-line script to get the value.

The text file

template.file
only contains:

$1


Here's an example of my utility:

vclient -h 10.0.0.131:3002 -t template.file -g getTempKist


What I would like is something like this:

vclient -h 10.0.0.131:3002 -t $(echo '\$1') -g getTempKist


Does anyone know how to use the result of an
echo
(or maybe an alternative) instead of an external text file?

I hope someone can help.

Markus

Answer

You can try two things:

First, you can probably use stdin for the input like this:

echo '$1' | vclient -h 10.0.0.131:3002 -t - -g getTempKist

Some tools support the special value - for filename arguments where - stands for stdin. However this depends on the implementation of the command.

Second thing you can use if your shell (like bash or zsh) supports this is to use process substitution:

vclient -h 10.0.0.131:3002 -t <(echo '$1') -g getTempKist