Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you insert a newline in between pipes in a shell script?

Tags:

bash

shell

For example, instead of:

( set -o posix ; set ) | grep -v '^PATH'| grep -v '^USER'

is there a way to do

( set -o posix ; set ) 
| grep -v '^PATH' 
| grep-v '^USER'

within the .sh script itself (in order to make the code cleaner and easier to read?

like image 320
yoursweater Avatar asked Nov 15 '25 04:11

yoursweater


1 Answers

Put the pipe at the end of the line instead of the beginning

( set -o posix ; set ) | 
    grep -v '^PATH' | 
    grep -v '^USER'

or escape the newlines

( set -o posix ; set ) \
    | grep -v '^PATH' \
    | grep -v '^USER'

If you use the second method, make sure there's no whitespace after the backslash, it has to be right before the newline.

Note that there's no need to use two grep calls. Use grep -E with a regexp that matches both variables.

( set -o posix ; set ) | grep -E -v '^(PATH|USER)'
like image 56
Barmar Avatar answered Nov 17 '25 20:11

Barmar