Run function from the command line
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
You usually cannot call an arbitrary function directly from the shell unless some program exposes that function as a command-line entry point. The practical solution is to wrap the function in a script, module, or executable that accepts arguments from the command line. In other words, the shell runs programs, and the program decides which function to call.
The Core Idea: Add A CLI Entry Point
Suppose you have a Python function:
The terminal cannot invoke greet by name on its own. You need a small command-line wrapper.
Save that as app.py, then run:
This is the normal pattern in scripting languages: the shell launches a program, and the program routes command-line arguments into the function.
Call A Specific Function By Name
If you want one file to expose several functions, add a command argument that selects the function.
Usage:
This is much more robust than trying to expose internal function names directly without validation.
A Lightweight One-Liner Option
For quick experiments, some languages let you evaluate code from the command line.
Example with Python:
Or import a module and call a function:
This is convenient for ad hoc use, but it is a poor long-term interface. Once the command becomes part of normal workflow, write a proper CLI wrapper.
Shell Functions Are Different
In a Unix shell, you can define a shell function and run it directly, but that function exists only inside the shell session or a sourced script.
This is not the same as calling a Python, JavaScript, or Java function from the terminal. It is just running a shell-defined function in the shell process.
That distinction matters because many "run function from command line" questions mix these two ideas.
For Reusable Tools, Prefer A Real Command
If the function will be used repeatedly by humans or automation, turn it into a proper executable command. In Python, that often means:
- a
main()function - argument parsing with
argparse - optional installation as a console script entry point
A real command gives you:
- validation of inputs
- predictable help text
- better error messages
- a stable interface for scripts and CI jobs
Security And Safety
Do not build a command-line interface that blindly takes a function name from user input and executes it with globals() or eval(). That pattern is fragile and unsafe.
A safer model is explicit routing:
- parse a known command
- map it to an allowed function
- validate each argument
That is more work than dynamic execution, but it prevents a large class of avoidable bugs.
Common Pitfalls
- Expecting the shell to know about functions that only exist inside source code.
- Using
python -cor similar one-liners as a permanent interface instead of building a proper CLI. - Executing functions by unchecked string name through
eval()orglobals(). - Forgetting argument parsing and type conversion, which leads to brittle commands.
- Confusing shell functions with functions defined in another programming language.
Summary
- The shell runs programs, not arbitrary in-memory language functions.
- To run a function from the command line, wrap it in a script or CLI entry point.
- For multiple functions, expose explicit subcommands instead of dynamic execution.
- One-liners such as
python -care fine for quick tests but weak as long-term interfaces. - Shell functions are a different mechanism from calling functions in Python, JavaScript, or other languages.

