Percent sign (%) in front of shell command

9

1

I just pasted a bunch of code into my shell, but forgot to remove the leading % before running the command. It appears to have done nothing. Why does this do nothing in my shell (zsh)?

> % echo foo

In bash, I get the following

> % echo foo
bash: fg: %: no such job

I just want to be sure that running my command hasn't had some side-effect.

Dave

Posted 2014-10-15T09:28:18.247

Reputation: 308

Answers

9

% refers to a 'jobid' which is used as part of the "job control and process control" features of zsh. To quote from the user-guide:

'fg' is the default operation on jobs referred to with the '%' notation,
so just typing '%1' with no command name would have worked, too.

Example:

$> man zshall &
[1]  + 31392 suspended (tty output)  man zshall
$> jobs
[1]  + suspended (tty output)  man zshall
$> %
<manpage-opens-for-zshall>

Read more about it here: http://zsh.sourceforge.net/Guide/zshguide03.html#l39

To answer your real question: If your zsh does not put a warning there ("fg: no current job" or the like) then there IS / WAS a job. Example:

$> sleep 60 &
$> % echo foo
[1]  + 411 running    sleep 60
fg: job not found: echo

The % just got back the background process and runs it. After 60 seconds it got the next 'jobid' which wasn't found.

Thus: The behavior you experienced is a bit strange. Without your full zshrc it's hard to tell what exactly is going on. It seems like you have managed to tell your zsh that the '%' char is starting a comment. I would check the histchars parameter for it's 3rd value:

$> echo $histchars

akira

Posted 2014-10-15T09:28:18.247

Reputation: 52 754

Brilliant, thanks. I was able to replicate your tests and foreground a running job with %. Good catch on the $histchars too, but mine is a hash sign (!^#). I'm guessing the answer to my particular case then is that there were some backgrounded processes behaving strangely in zsh. Thanks again for the shell lesson! – Dave – 2014-10-15T11:16:41.697