This question already has an answer here:

How would I make a pipe insert characters into the middle of a string? Here's my example (running Raspian):

hostname -I | sudo python ~/testsite/ runserver {the ip from the first command}:80

Is this possible? Also, please note that I would not like to use a static IP.

marked as duplicate by dr01, roaima, cuonglm bash Nov 18 '15 at 17:25

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    sudo python ~/testsite/ runserver "$(hostname -I)" – jordanm Nov 18 '15 at 14:43
  • @jordanm: Also, can I use pipes within "$(hostname -I)"? For me, hostname -I returns an IPv4 and an IPv6 address. I have made a command to cut out the IPv6. Can I attach that to the hostname -I command? – Colby Gallup Nov 18 '15 at 15:07
  • @LehGogh it depends what is the command, but mainly Yes. You can try also hostname -i not hostname -I (lower case "i") . – NIZ Nov 18 '15 at 15:39
  • @LehGogh you can use sudo python ~/testsite/ runserver "$(hostname -I | your_custom_command)" just fine – Dmitry Grigoryev Nov 18 '15 at 16:45
  • @NIZ hostname -i returns the localhost IP for me. – Colby Gallup Nov 18 '15 at 16:56

I don't think you want to do what you're asking to do. If all you want is the output of one command used as the argument of another command, then structures like $(...) or `...` are your friend. For example:

sudo python ~/testsite/ runserver $(hostname -I):80

Note that this is NOT what you asked for, but it does what you want.

If you really want stdin (a pipe) to be used as an argument, you may be able to use xargs(1) or a quick while loop in your shell.

This puts stdin at the end of the line:

echo Hello | xargs echo "The word is:"

or if you want to substitute something inside the line:

echo Hello | xargs -J % echo % is the word.

But xargs can be tricky to use if you're not able to understand its (somewhat arcane) usage. The xargs command also varies from OS to OS; the -J option works in BSD and OSX, but not in some older Linux distros. I don't know what OS you are using.

A loop might be easier:

echo Hello | while read word; do echo "$word is the word."; done

Or in your case:

hostname -I | while read ip; do
    sudo python ~/testsite/ runserver ${ip}:80

The loop only gets run once, but you can put together your command line a little more easily. YMMV.

  • I don't quite understand what benefits does your approach have over command substitution as suggested by jordanm. – Dmitry Grigoryev Nov 18 '15 at 16:44
  • I prefer jordanm's answer over this. I don't see the point in looping over a single word. – Colby Gallup Nov 18 '15 at 16:50
  • I'm using Raspian (a version of Debian). – Colby Gallup Nov 18 '15 at 16:56
  • I prefer jordanm's solution over this also, but your question was explicit about using a pipe, so that was the answer I provided. Given this clarification that you're open to solutions beyond what you asked for, I've updated my answer. As for the point of running a single loop, as I said in the answer, using a loop allows you to use a variable as your stdin rather than having to use xargs' arcane non-portable character replacement technique. – ghoti Nov 18 '15 at 18:03

I think you can use a dash which stands for the standard output from the previous command "hostname -I"

hostname -I | sudo python ~/testsite/ runserver -

If you want to cut ipv6:

hostname -I | some command to cut ipv6 |sudo python ~/testsite/ runserver -
  • dash must be explicitely supported by for this to work. Also, appending ':80' to the output of hostname would be problematic. – Dmitry Grigoryev Nov 18 '15 at 15:15
  • I think it is not related to The dash in the pipeline has a special meaning. I think before is executed, the dash will be replaced by the ipv4 obtained by previous commands. – bitsoal Nov 18 '15 at 15:30
  • I certainly don't think it's true. Do you have a reference on dash having a special meaning on command line? – Dmitry Grigoryev Nov 18 '15 at 15:35
  • 1
    The dash is an argument to the command it follows. It's not a special character, like a variable or a pathname, which can be expanded into something else. There is a convention to allow the dash to denote a file that is actually stdin, but it is by no means universal, or enforced. Also, if supported, the dash would allow stdin to be the INPUT to the command, not an OPTION to it. – ghoti Nov 18 '15 at 16:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.