Mind it does not escape ! (exclamation mark). So if you want to i.e. printf() commands for later use in shell (i.e. by pasting to the console) you need to escape all exclamation marks or shell will try to process ! as history reference. This approach shall suffice:
<?php $scaped = str_replace('!', '\!', escapeshellarg( $str ) ); ?>