1

I'm trying to execute a shell command from php using the exec command

The following command works

echo exec('whoami');

but I need to execute the following shell command

cat > /var/www/myfolder/abc.txt
hi
hello
welcome

the problem is that the above shell command has to be executed in multiplelines. The words "hi", "hello", "welcome" are dynamic and come from php variables. Is there a workaround for this problem.

1 Answer 1

1

To execute a multiline shell command in php:

passthru("bash <<'END'
        echo \"executed in new bash session\";
        pwd;
        whoami;
        exit;
        END
");

This way you could for example connect to a ssh server using php and then execute some commands there

I don't understand what you mean by cat > abc.txt. This command will empty the file.

Sign up to request clarification or add additional context in comments.

1 Comment

the cat command is used to write content to a file in terminal when I provide cat >abc.txt hI hello (ctrl+D) a file is created with hi hello as its contents

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.