- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
io.popen() function in Lua Programming
Sometimes we want to execute the system's command and then make use of whatever was returned by them, and in order to do that we simply can either make use of the os.execute() function or io.popen() function.
The difference between the os.execute() function and the io.popen() function is that the output value of the os.execute() function is much harder to deal with, and that is the reason why it is recommended to use the io.popen() function, whose output value is much easier to handle and make use of.
io.popen() starts the program in a separate process and returns a file handle that you can use to read data from this program.
output = io.popen(command)
Now that we know what io.popen() function does, let’s use it in a Lua example.
Consider the example shown below −
local handle = io.popen("echo hello") local result = handle:read("*a") handle:close()
In the above code, we made use of io.popen that returns a file handle that we can use to read the output of the command.
Kickstart Your Career
Get certified by completing the courseGet Started