DEV Community

Cover image for Build a simple CLI with Bash
Adiatma Kamaarudin
Adiatma Kamaarudin

Posted on

Build a simple CLI with Bash

Bash is a unix shell and command language, and this article has a purpose to introduce how to using bash for building a simple CLI (Command Line Interface).

Prerequisite

Getting Started

Please create a new folder with the name bash-cli then create a file with the name bash-cli.sh inside of bash-cli folder, and open the file bash-cli.sh.

#!/bin/bash

echo "Hello World"
Enter fullscreen mode Exit fullscreen mode

#!/bin/bash line of script to inform your machine os that is a bash scripting.

Next, we need to make sure the scripts work. you just need to execute with source ./bash-cli.sh inside of bash-cli directory, or you can access directly ./bash-cli but you need to enable access permissions with command chmod, simply just type chmod +x ./bash-cli.sh.

./bash-cli.sh
Hello World
Enter fullscreen mode Exit fullscreen mode

Yes, congratulations bro, and next we need to add more syntax to build a simple project CLI.

#!/bin/bash

# catch first arguments with $1
case "$1" in
 -s|--scan)
  # execute if arguments in $1 to equal "-s" or "--scan"
  echo "scan..."
  ;;
 *)
  # else
  echo "Usage: (-s|--scan)"
  ;;
esac
Enter fullscreen mode Exit fullscreen mode

if we execute the bash with ./bash-cli.sh -s the result to showing text "scan..." in your terminal.

./bash-cli.sh -s
scan...
Enter fullscreen mode Exit fullscreen mode

next, add more functionality to scan something on the internet, or we can use curl to fetch or download data from the internet.

#!/bin/bash

scan_your_ip() {
  command curl "https://ipecho.net/plain"
}

# catch first arguments with $1
case "$1" in
 -s|--scan)
  # execute if arguments in $1 to equal "-s" or "--scan"
  echo scan_your_ip
  ;;
 *)
  # else
  echo "Usage: (-s|--scan)"
  ;;
esac
Enter fullscreen mode Exit fullscreen mode

Finish, let's try with execute ./bash-cli.sh -s and expected you get your network ip as result, like this.

./bash-cli.sh -s
36.75.151.87
Enter fullscreen mode Exit fullscreen mode

Conclusion

I hope you need basic experience in using bash for building a simple CLI, for more reference you can build a more complex CLI, please check out this repo https://github.com/adiatma/intip as references.

Top comments (3)

Collapse
 
trueneu profile image
Pavel Gurkov

You might be interested in using getopts, a bash built-in, instead of re-inventing the mech yourself.

Collapse
 
adiatma profile image
Adiatma Kamaarudin

Hi Pavel, thanks for the suggestions, the article just has a purpose to introduce the basic way to using bash script.

Collapse
 
trueneu profile image
Pavel Gurkov

No worries.
I understand it's introducing the basic way. It's just, you have to take it only a little bit further to run into problems your article doesn't cover. Instead of using a standard, well-documented way, you're re-inventing the wheel with no pros. For instance, if I have more than one option, what do I do? If my options take values, what do I do? Also, the way you've described doesn't respect the POSIX convention (only arguments that start with a hyphen are options, may be clustered etc) (gnu.org/software/libc/manual/html_...).