$path as written would be a shell or environment variable. You can pass Rust variables with arg. The easiest and safest way to do this is to get rid of the bash -c invocation and call mv directly. Then you can pass &path as a separate argument:
let path = "/home/directory/".to_string();
let exit_code = Command::new("mv")
.arg("somefile.txt")
.arg(&path)
.spawn()
.expect("Cannot be executed")
.wait()
.expect("Wait failed");
Playground
If you did need the explicit shell invocation for some reason, the naïve approach might be to use manual string building, say with format!, to build a shell command:
// Unsafe! Do not build commands by hand.
let exit_code = Command::new("sh")
.arg("-c")
.arg(&format!("mv somefile.txt {path}"))
...
Don't do that! It is unsafe in the same way that building SQL strings by hand leads to SQL injection vulnerabilities. If path were a file name with spaces it would be split into several file names. Worse, an attacker could inject a path like ; rm -rf / and wipe out your server.
The safe way to pass arguments is still using separate arg invocations. The trick is to reference them as "$1", "$2", etc., in the shell command. This method ensures that spaces, quotes, and other tricky characters won't cause problems.
let exit_code = Command::new("sh")
.arg("-c")
.arg("mv somefile.txt \"$1\"")
.arg("sh") // $0
.arg(&path) // $1
.spawn()
.expect("Cannot be executed")
.wait()
.expect("Wait failed");
Playground
Also, don't use let _command =. That would assign the final Result to a variable without checking whether it was successful or not. It's a good habit to always check Results. You can see I added a second expect call in both of the snippets above.
But then, calling unwrap and expect too often is another bad habit. Whenever possible you should aim to propagate errors. The ? operator makes this easy. It's shorter and nicer for callers:
fn main() -> io::Result<()> {
let path = "/home/directory/".to_string();
let exit_code = Command::new("mv")
.arg("somefile.txt")
.arg(&path)
.spawn()?
.wait()?;
Ok(())
}
Playground