I want to know what happens when this command is executed?

cd / | vi


This is the command I typed in the bash shell. When running this command, it just opens vi editor. And then I went away using: q. I am now in bash shell. when we enter any command here, it will not be displayed, but the command is executed. I want to know where it is redirected.


source to share

1 answer

Not 100% sure, but I would like to give my opinion on this.

I think it has something to do with I / O control.

read the man page cd


       If a non-empty directory name from CDPATH is used, or if cd − is 
used, an absolute pathname of the new working directory shall be written to the 
standard output
           as follows:

           "%s\n", <new directory>

       Otherwise, there shall be no output.


those. if you cd /

are then stdout is equal /dev/null

and pipe makes stdout as stdin for vim.

When vim starts up, it saves terminal settings, I / O controls. But if it was /dev/null

, then the tty file descriptor vim

will not have the correct settings. And after you exit vim this will apply the right things to your terminals.

To test, do the following:

cd -|vim


cd -

will have stdout then vim will have normal stdin, after exiting vim the terminal will still work fine.

Then try this:

vim </dev/null


It happens in the same way as cd /|vim

, after exiting vim, your terminal behaves strangely, just likecd /|vim

stty sane


can save your terminal.



All Articles