Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In bash, how to process all user input on command line

How to make all user input on command line as stdin for a program?

In my case, I want to replace certain words inputed by user. For example, every time user uses the word animal1, I want it received as goldfish. So it would look like this:

$ animal1
goldfish: command not found

I tried the following bash command

while read input
do
   sed "s/animal2/zebra/g;s/animal1/goldfish/g" <<< "$input"
done

But it prompts for user input and does not return to bash. I want it to run while using bash command line.

Also, this allowed me to capture output only.

bash | sed 's/animal2/zebra/g;s/animal1/goldfish/g'

But not user input.

like image 989
akoshak Avatar asked Dec 03 '25 07:12

akoshak


1 Answers

If I understand you correctly, sounds like you just need to set up some aliases:

$ alias animal1=goldfish
$ animal1
bash: goldfish: command not found

This allows the shell to be used interactively as usual but will make the substitutions you want.

You can add this alias definition to one of your startup files, commonly ~/.bashrc or ~/.profile, to have them take effect on any new shell that you open.

like image 136
Tom Fenech Avatar answered Dec 04 '25 22:12

Tom Fenech