capture the command that launched the running process in my shell

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



capture the command that launched the running process in my shell



I try to make the title of my windows in GNU screen automatically equal to the path of the working directory PLUS the process running if there is one (e.g: npm start or vim file.js)



for that purpose I added these lines in my .zshrc :


precmd ()
local action = action_to_define
if [[ $TERM == screen* ]]; then
printf -Pn 'ek%~ $actione\'
fi



this send (somehow) the path as a title to screen (see this post)



and the variable action would print the running program if it exist



I tried local action= $(history | tail -1 | sed 's#[0-9 ]*##') because this select the prompt of the last command in the history (just like history !! would do if the option !! was recognized, which is not for some reason...)


local action= $(history | tail -1 | sed 's#[0-9 ]*##')


history !!


!!



and local action= $(ps -lr | tail -1 | sed 's#^.*:...##') because this select the command of the running process


local action= $(ps -lr | tail -1 | sed 's#^.*:...##')



but it doesn't works, as if the process was not captured neither by history or ps... maybe precmd run before the action is launched, so I tried other functions like preexec or zshaddhistory without any luck...


precmd


preexec


zshaddhistory




1 Answer
1



The precmd hook is only run after a command finished, just before the next prompt is displayed. So it can be used to change the terminal title for when the prompt is shown. In order to change the terminal title when a command is run, you need the preexec hook, which is run after a command was accepted (after hitting Enter, just before the command is run.


precmd


preexec



When confirming a command on the prompt, this command is then passed to preexec as arguments in three forms


preexec



So this should do the trick:


preexec ()
local action="$1"
if [[ $TERM == screen* ]]; then
printf -Pn 'ek%~ $actione\'
fi



As precmd hook is only run, before prompting the next command, it can be simplified:


precmd


precmd ()
if [[ $TERM == screen* ]]; then
printf -Pn 'ek%~e\'
fi



Although it is not the main reason why it would not work, it is important to note that in Zsh (and most other Unix shells) there must be no spaces before or after = when assigning values to parameters.


=



The exact behavior might differ, depending on where you put spaces and whether you use something like local or set or just plain assignments.


local


set



To use the code from the question as example:



putting spaces before and after = with local will usually result in a bad assignment


=


local


% local action = action_to_define
zsh: bad assignment



putting spaces only after = with local will assign an empty string to the variable and either create/set another empty variable or result in a context error, depending on whether the intended value would be a viable parameter name:


=


% local action= "echo"



This empties action and echo


action


echo


% local action= "echo foo"
local: not valid in this context: echo foo



Here action is emptied, too, but local fails after because echo foo is not a viable parameter name due to the included space.


action


local


echo foo





great ! preexec works ! and good to know for the spaces around =
– hugogogo
Aug 14 at 10:44


=






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered