The cd terminal command does not work with Scala script

I need to run a shell command from a Scala script, and for this I use the following snippet:

import scala.sys.process.{Process, ProcessIO}

val command = "ls /tmp"
val process = Process(command)

val processIO = new ProcessIO(_ => (),
    stdout => scala.io.Source.fromInputStream(stdout).getLines.foreach(println),
    _ => ())
process.run(processIO)

The code is working fine. I wonder why I get

java.io.IOException: Cannot run program "cd": error=2, No such file or directory

as soon as I change the command to cd /tmp && ls, which is equivalent to IMO for ls /tmp?

+5
source share
1 answer

From Wikipedia to the teamcd :

[...] on Unix systems cdcalls the chdir()POSIX C function . This means that when the command is executed, a new process is not created to migrate to another directory, as is the case with other commands, such as ls. Instead, the shell itself executes this command.

There is even a quote about Java:

[...] Java, Java chdir(); , , Java, , 2008 [...]

:

$ which ls
/bin/ls
$ which cd
$

cd (), (, /bin/ls) - .

? Java? ?

+8

All Articles