Skip to content

Instantly share code, notes, and snippets.

@postpostscript
Last active November 7, 2024 02:47
Show Gist options
  • Save postpostscript/bb88e3dad565c0d8ee54031f6b758a09 to your computer and use it in GitHub Desktop.
Save postpostscript/bb88e3dad565c0d8ee54031f6b758a09 to your computer and use it in GitHub Desktop.
replify - Create a REPL for any command
#!/bin/sh
command="${*}"
printf "Initialized REPL for `%s`\n" "$command"
printf "%s> " "$command"
read -r input
while [ "$input" != "" ];
do
eval "$command $input"
printf "%s> " "$command"
read -r input
done

HN Discussion

$ replify echo
Initialized REPL for [echo]
echo> "Hello, World!" 
Hello, World!

echo> "It works!"
It works!

$ replify git
Initialized REPL for [git]
git> init
Initialized empty Git repository in /your/directory/here/.git/

git> remote add origin https://your-url/repo.git                

git> checkout -b new-branch
Switched to a new branch 'new-branch'

git> push   
#!/bin/sh
git clone https://gist.github.com/bb88e3dad565c0d8ee54031f6b758a09.git ./replify && cd ./replify && mv ./" replify" ./replify && sudo chmod +x ./replify && sudo ln -s `pwd`/replify /usr/bin/replify

See mchav/With for a similar tool that has more features.

@nwoeanhinnogaehr
Copy link

Using rlwrap is another option. Just do rlwrap replify ...

@habibalamin
Copy link

habibalamin commented Aug 22, 2016

@nwoeanhinnogaehr ah, that's what I meant. I'm not sure why I said readline, that doesn't even exist (as a binary on the PATH) for me. You will have to install rlwrap as well, though (at least, I do).

@trapier
Copy link

trapier commented Mar 31, 2017

Took a shot at a different approach by starting a subshell with <Enter> bound to <Enter>$1<Space>. This approach provides readline and bash tab completion. Haven't figured out how to remove the need to hit <Enter> once to get things going, but this isn't annoying enough to keep me from using it. Linking a gist rather than posting in-line here because it contains a ^M control character which doesn't copy-paste well.

https://gist.github.com/trapier/887ac740ba0d68f9d1dab87a7d6fe253

Example in use:

$ ./replify.sh docker
docker> 
docker> docker 
build      create     export     images     kill       logs       plugin     push       rmi        secret     stats      tag        version    
commit     diff       help       import     load       network    port       rename     run        service    stop       top        volume     
container  events     history    info       login      node       ps         restart    save       stack      swarm      unpause    wait       
cp         exec       image      inspect    logout     pause      pull       rm         search     start      system     update     
docker> docker run -d nginx
eeb6d1ed4d273c345bf726394ae9cf3897f58c1d395d8e5150400bab1467545f
docker> docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
eeb6d1ed4d27        nginx               "nginx -g 'daemon ..."   2 seconds ago       Up 1 second         80/tcp, 443/tcp     nostalgic_leavitt
docker> docker ^C
docker> exit
$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment