Trying to run:

function which_terminal {
        return (ps -p$PPID | awk "'NR==2'" | cut -d "' '" -f 11)

inside .zshrc to get a varible with which terminal emulator is running so I can configure different themes for different terminal emulators.

when I run this commmand in command line I get exactly the emulator being used. But when I try to add this to the file I get a error like:

which_terminal:1: no matches found: (ps -p16632 | awk 'NR==2' | cut -d ' ' -f 11)

can't find where it's wrong, if anyone can help

  • Apart from whatever zsh syntax you've invoked, the awk program will output all rows of the ps output since the string NR==2 is "true". Drop the inner set of double quotes.
    – Kusalananda
    Jun 19 '18 at 19:06

In the shell, the return value of a function is like the exit status of a command: you can only return a small integer value indicating success (0) or a failure code (> 0).

This status has nothing to do with the output of a command. To run a command and store its output into a variable, use command substitution. To run a command in a function and gets its output as the output of the function, just run the command.

function which_terminal {
    ps -p$PPID | awk "'NR==2'" | cut -d "' '" -f 11

Note that parsing the output of ps is unreliable and overkill. (Plus you got it wrong: you're parsing 'NR==2' to awk and ' ' to cut, both of which are invalid arguments; you need either single quotes or double quotes around each of these, not both.) The ps command has options to print out whatever field you want, e.g. comm for the name of the executable (truncated to 16 characters under Linux) or args for the full command line with arguments (and sometimes the full path to the command, depending on how it was invoked).

function which_terminal {
    ps -p$PPID -o comm=

Or, to store the output into a variable:

parent_process_command=$(ps -p$PPID -o comm=)

The reason for the error you're getting is that zsh is trying to parse what follows return as an argument of a command (here it's an argument of the return keyword, but they're parsed in the same way). It sees an open parenthesis and decides that it's the start of a wildcard expression. That wildcard expression would match a file called ps -p16632, awk 'NR==2' or cut -d -f 11 (with leading/trailing spaces that don't render here), but since you (unsurprisingly) have no files with any of these names, zsh complains that it didn't find a match. return ? would return 3 if you happened to have a file called 3 in the current directory and no other file with a one-character name.

  • thank you so MUCH for your answer. Just one thing, is it possible to "ps cmd=" return just the name of the terminal? It would be nice Jun 19 '18 at 19:28
  • @ArthurCaccavo What are you calling “the name of the terminal”? Jun 19 '18 at 22:30
  • the command, without the double quotes, was returning in the terminal line just the name of the terminal, "tilix" or "gnome-terminal". The ones I use Jun 21 '18 at 1:10
  • @ArthurCaccavo Your commend was truncated, but now I think I see what you mean: you may prefer comm to cmd. Jun 21 '18 at 6:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.