Piping Bash

Bash is good, ZSH is also good….but the fact that they both do not allow piping is bad.

Well technically they do work, but it works in a different way.

Sometimes its okay for us, rest times it is not.

So what’s the issue here…

Say you have a code in your bash script like this

apt install fail2ban
read -sp "Enter your Name : " name
sed -i "/name =/ s/$/$name/" /etc/hostname

The above code can work if we save it inside a script.sh and also normally when piping..but it does not work as we expected [Does not wait and get the input from read instead it goes and skip to the next command regardless of what the previous command was]

This is because when you pipe it to bash…it does execute it one by one and not line by line…when these both can seem similar…it is clearly not.

You can think this like a rude boss who does not show respect but needs tons of respect. Say you say good morning when your boss comes in but he just walks away

But when you do the same when saving it in a script its like giving a handshake to your boss and then him waiting and shaking back until you both mutually leave hand.

There are fixes tho. like you can add read -sp "Enter your Name : " name < /dev/tty to link it with a terminal input.


Leave a Reply

Your email address will not be published. Required fields are marked *