Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua change current/working dir, Linux (without LFS or any non-std modules)

Tags:

dir

lua

I need to change current working dir in lua script for execute specific actions, but i have a trouble with this simple task. I write test script test.lua :

os.execute("cd /usr")
os.execute("ls")

But lua test.lua output is:

test.lua

Current dir doesn't change. What's is wrong? And i can't use LFS or any non-std modules.


Thanks to all for explaining it situation. I choose another way : change work dir before run lua script, but i have a lot of troubles with paths which use in scripts and with scripts.

like image 993
Reddy Avatar asked Aug 01 '26 16:08

Reddy


2 Answers

On Unix, os.execute() spawns a child process and its children. In jpjacobs's answer, the first process would execute the shell.

The directory change operation only affects the child process, not the process in which the Lua interpreter is executing your program.

like image 78
hillu Avatar answered Aug 04 '26 07:08

hillu


It's simply not possible without external libraries. You can use stuff like

os.execute("cd /usr/ && ls")
like image 39
jpjacobs Avatar answered Aug 04 '26 06:08

jpjacobs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!