Notes about various (technical) topics and encountered or solved puzzles from engineering, devops or networking disciplines.

Search This Blog

Sunday, March 30, 2014

How to automatically prefill command on the Linux bash

Linux Bash is one of the most famous Linux shells. It offers a great number of features like for example spawning and controlling process, redirecting streams, supporting scripts and a flexible way to control you editing line.

Problem

How to automatically pre-populate a command on the shell after prompt.

Solution description

The shell has tree default streams: stdout, stdin and stderr. By manipulating the stdin of the process we can simulate typing a command.

# ./type-command
type-command: the variable TYPE_CMD_ENABLED is not set, set it to 'no' to surpress this message; set the TYPE_CMD_TYPE for the command to type
Example: export TYPE_CMD_ENABLED=yes; export TYPE_CMD_TYPE=date

Export the variable to controls if the program should try to type a command or not

Last posts

About Me

Linux enthusiast

Profile:Curious systems engineer interested in many of the IT technologies but especially in cloud systems and network engineering. A quick learner who likes to tinker and who often spent time researching and trying new technologies for personal and business benefit.

Please note that the code available here is only for demonstration purposes. If you want to be serious, you'll have to make it more robust and integrate it. Also, the description is by no means a definitive reference on any of the subjects, but rather the result of my experimentation. Feel free to report any bugs or errors you find in the code or otherwise in the articles. Thanks