Use of the PASTE command in a script file

Hi,

When I use the paste command in the terminal window it works fine, but when i try to use it in a bash script file i get errors. I'm not sure how to use the paste command in a bash script file.

my paste command looks like this

paste <( code1 ) <(code2) 

thanks

How do you call your script? How are you ensuring that bash is used to execute the script and not some other shell?

This what my bash looks like

#!/bin/bash
echo $(paste <( code1 ) <(code2))

and i use this to execute it

sh test.sh

thx

sh is not the same as bash , so either use:

bash test.sh

or

./test.sh

To be able to execute the latter command you need to make the script executable first using chmod .

1 Like

Hmm i tried with the bash test.sh and works as expected. So i assume it's not possible to make the paste code work using the sh command of the bash ?

thx

bash provides many functionalities that sh does not, for instance "process substitution" that you use in your code sample.
You need to output to two files, and paste those, or you need to play dirty tricks, like

$ mkfifo J
$ { code1; } | paste - J &      # paste stdin and the FIFO; put in background
$ { code2; } > J                # print to FIFO
$ rm J
1 Like

Interesting, ok thank you guys. :smiley:

Generally, I don't consider using fifo's to be in any way dirty. However, the way you're using one here is unreliable. The successful completion of your approach depends on several factors:

  1. The shell's execution environment. If job control is enabled, each background job runs in its own process group. Background process groups may not be able to write to the terminal.
  2. Terminal settings. If tostop is enabled (see stty), writes to the terminal by a process not in the foreground process group triggers SIGTTOU signals to all processes in the process group.
  3. Process signal masks. If the writing process is blocking or ignoring SIGTTOU, then the signal isn't sent to the process group.

The following approach could be stopped as soon as paste tries to write.

mkfifo f1

cat <<EOF | paste - f1 &
1
2
3
EOF

cat <<EOF >f1
a
b
c
EOF

For a more reliable approach, I would suggest using a second fifo:

mkfifo f1 f2

cat <<EOF >f1 &
1
2
3
EOF

cat <<EOF >f2 &
a
b
c
EOF

paste f1 f2

Regards,
Alister