How to show quoted command list?
On Linux, you can get a slightly more raw list of args to a command from /proc/$pid/cmdline
for a given process id. The args are separated by the nul char. Try cat -v /proc/$pid/cmdline
to see the nuls as ^@
, in your case: xss-lock^@--notifier=notify-send -- 'foo bar'^@slock^@
.
The following perl script can read the proc file and replace the nuls by a newline and tab giving for your example:
xss-lock
--notifier=notify-send -- 'foo bar'
slock
Alternatively, you can get a requoted command like this:
xss-lock '--notifier=notify-send -- '\''foo bar'\''' 'slock'
if you replace the if(1)
by if(0)
:
perl -e '
$_ = <STDIN>;
if(1){
s/\000/\n\t/g;
s/\t$//; # remove last added tab
}else{
s/'\''/'\''\\'\'\''/g; # escape all single quotes
s/\000/ '\''/; # do first nul
s/(.*)\000/\1'\''/; # do last nul
s/\000/'"' '"'/g; # all other nuls
}
print "$_\n";
' </proc/$pid/cmdline
As meuh noted, you can get the string on Linux and NetBSD from /proc/PID/cmdline
with arguments delimited by NUL bytes. Here is a quick and dirty way to transform them into runnable command-lines.
perl -ne 'print join(" ", map quotemeta, split(/\000/)), "\n"' /proc/.../cmdline
The output looks like:
xss\-lock \-\-notifier\=notify\-send\ \-\-\ \'foo\ bar\' slock
You can directly copypaste it to your shell to run it.
Shorter variant (requires Perl 5.10 or newer):
perl -nE '$, = " "; say map quotemeta, split /\0/' /proc/.../cmdline
And while I'm at it, a golfed version (40 bytes):
perl -nE'$,=" ";say map"\Q$_",split/\0/' /proc/.../cmdline