xargs with command that open editor leaves shell in weird state

前端 未结 5 877
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-04 04:27

I tried to make an alias for committing several different git projects. I tried something like

cat projectPaths | \\
xargs -I project git --git-dir=project/.gi         


        
5条回答
  •  独厮守ぢ
    2021-02-04 04:59

    Using the simpler example of

    ls *.h | xargs vim
    

    here are a few ways to fix the problem:

    xargs -a <( ls *.h ) vim
    

    or

    vim $( ls *.h | xargs )
    

    or

    ls *.h | xargs -o vim
    

    The first example uses the xargs -a (--arg-file) flag which tells xargs to take its input from a file rather than standard input. The file we give it in this case is a bash process substitution rather than a regular file.

    Process substitution takes the output of the command contained in <( ) places it in a filedescriptor and then substitutes the filedescriptor, in this case the substituted command would be something like xargs -a /dev/fd/63 vim.

    The second command uses command substitution, the commands are executed in a subshell, and their stdout data is substituted.

    The third command uses the xargs --open-tty (-o) flag, which the man page describes thusly:

    Reopen stdin as /dev/tty in the child process before executing the command. This is useful if you want xargs to run an interactive application.

    If you do use it the old way and want to get your terminal to behave again you can use the reset command.

提交回复
热议问题