18

This question already has an answer here:

Test environment:

$ mkdir testdir
$ cd testdir
$ echo | tee "file 
name"

Now, ls will print file?name (under Ubuntu GNU bash, at least), while ls | cat will print file and name in separate lines.

What I'd like to achieve is, print file?name with the piped version, so something like

$ something ls | cat` or `ls | something | cat` or `ls | something cat

In other words, how to fool a command like ls here into thinking, it has a TTY, when it does not, in the shell?

Note: ls is just an example program here, I'm looking for generic solution, not program specific like ls -q | cat would be.

marked as duplicate by sam, jayhendren, Kusalananda, Eric Renouf, taliezin Jan 6 '17 at 19:03

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.

  • 5
    This is an older question than the nominated examplar, but as that question's answers include this question's answer, and more, I've nominated this older question for closure. – Wayne Conrad Jan 6 '17 at 16:19
16

You can use socat to simulate a pseudo terminal (pty):

socat - EXEC:'ls --color=auto',pty,setsid,ctty | cat

There are many more option, see its documentation.

ls uses by default the width of the terminal for columnwise output. I found not yet a way, how to set this on a socat pty, a workaround is to use the ls option -w WIDTH.

  • 1
    You can do stty rows X cols Y inside the session to set the size of the terminal. Note that tput lines and tput cols query STDERR, while stty sets the state on STDIN, so make sure you pass the stderr option to socatas well if you want to checkthat way (this confused me at first !) – wump May 28 at 16:27
  • I'm constantly amazed by the power of socat. I don't need it often. It only seems to come up when I'm attempting the impossible, but it never disappoints. – ivan May 29 at 0:46

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