How to change terminal working directory using cli script?

I would like to change the directory in a Linux terminal using a cli script, not the current PHP working directory - hopefully using shell_exec ().

Example: from user@host:~$touser@host:/the/other/directory$

system()and exec()not allowed.

In my case, this does not work:

$dir = '/the/other/directory';
shell_exec('cd '.$dir);

and these

shell_exec('cd '.escapeshellarg($dir));
shell_exec(escapeshellcmd('cd '.$dir));
pclose(popen('cd '.$dir));

But shell_exec('ls '.$dir)gives me a list in this directory. Any cheaters?

+3
source share
3 answers

When you execute a few commands, how do you do:

shell_exec('cd '.escapeshellarg($dir));
shell_exec(escapeshellcmd('cd '.$dir));

This will not work. The first command has nothing to do with the second, so you cannot use the results of the 1st command to execute the 2nd command.

If you want to execute a chain of commands, use a pipe symbol |, for example:

echo shell_exec("ls /etc/apache2 | grep fileName");
+2

, shell_exec, php, ls . php, bash, , .

, bash . bash , php:

system('gnome-terminal --working-directtory=/home/doom');

, script , .

0

If you want to change the parent directory of the shell by calling php cli script, then you cannot do this. What you can do at best:

shell> eval `your/cli/script`

and in the script do something like

<?php
...
    echo "cd $dir"

?>
0
source

All Articles