Find the answer to your Linux question:
Results 1 to 6 of 6
Hi guys, I'm trying to get to grips with pipe. Often I want to look at the python code behind one of my custom scripts in the terminal. Usually I ...
  1. #1
    Just Joined!
    Join Date
    Feb 2012
    Posts
    3

    Piping problems

    Hi guys, I'm trying to get to grips with pipe.

    Often I want to look at the python code behind one of my custom scripts in the terminal. Usually I type "which myCommand", select the full path given, and use "cat".

    From what I've read, a pipe "|" passes the output of the left hand side to the input of the right. So why doesn't this work?

    Code:
    which myCommand | cat
    Thanks!

    Harry

  2. #2
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,096
    Because the pipe connects to stdin, but in this case you need to pass the path/filename as an argument.

    Try this
    Code:
    which myCommand |xargs -l1 cat
    You must always face the curtain with a bow.

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Although Irithori's command does work (xargs takes stdin and uses it as commandline arguments), it is not the best way to achieve what you want.

    You want to replace a part of a commandline with the output of another command. The standard way of doing this is:
    Code:
    cat $(which myCommand)
    DISTRO=Arch
    Registered Linux User #388732

  4. #4
    Just Joined!
    Join Date
    Feb 2012
    Posts
    3
    Hi guys, thanks for your answers.

    The xargs way works fine. However, when I use
    Code:
    cat $(which myCommand)
    I get a "Illegal variable name." I did hit on some success with this:

    Code:
    cat `which myCommand`

  5. #5
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,096
    The $() construct works in bash.
    And I agree with Cabhan, that it is nicer to read.

    Which shell do you use?
    You must always face the curtain with a bow.

  6. #6
    Just Joined!
    Join Date
    Feb 2012
    Posts
    3
    That would explain it; I'm using tcsh.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...