đž Archived View for idiomdrottning.org âş three-different-kinds-of-commands captured on 2023-11-14 at 08:39:58. Gemini links have been rewritten to link to archived content
âŹ ď¸ Previous capture (2023-01-29)
-=-=-=-=-=-=-
Remember how echo had the simple job to print back anything you told
it? Itâs time to meet another friend, which. Itâs more specific in what it wants to hear from you. If you tell it the name of another program it tells you how it knows that program. What does that mean? Letâs look. Iâve only told you about echo so far, so letâs ask which about echo like this:
$ which echo /bin/echo
That means that it knows echo is in the /bin directory, or at least thatâs where it is here on ellen, my computer. It might be different on yours.
Here, bash reports the same as the old Bourne shell, that itâs in /bin. On zsh, it looks like this:
ellen% which echo echo: shell built-in command
So some programs, or âcommandsâ, are so familiar to the shell that theyâre built in.
Remember how echo had the simple job to print back anything you told
it?
Letâs ask which about which.
$ which which /usr/bin/which
So on the classic Bourne shell, which is in the /usr/bin directory. The same is true on the bash shell, while zsh also has a built in which. See:
ellen% which which which: shell built-in command
To round this out, Iâll show you what which reports in my zsh here on my computer when I ask it about the three shells Iâve mentioned so far.
ellen% which sh /bin/sh ellen% which bash /bin/bash ellen% which zsh /usr/bin/zsh
Again, Iâm typing what follows the %-signs, and zsh is typing the rest back to me, both the prompt ellen% and the answers to the questions it got from which.
So which expects the name of programs and wonât understand any other words.
ellen% which way to the top way not found to not found the not found /usr/bin/top
Itâs not smart. It believes that way, to, the and top are the names of programs, not parts of a sentence. Turns out itâs actually right about top, though. Itâs a classic program that shows you how much work the computer is doing right now. If you happen to start it and donât know what to do, you can press q to leave it. And what do I know, maybe you have programs named âwayâ, âtoâ or âtheâ installed? I didnât.
Thatâs not to say that echo is smart about what you tell it either. It can parrot it back, but it canât âunderstandâ it.
So far, Iâve been pretty careful to only use words with plain letters in my examples of âtalkingâ to the shell. Even though Iâve asked questions like âCan I just type anythingâ and âwhich way to the topâ, I havenât even used a question mark.
The reason for this is that the shell itself (rather than programs, like echo or which) has a special relationship to some of the special characters like ?, *, # and a few others, especially apostrophes and quotation marks.
For example, the # symbol means something that the shell just throws away and does not pass along to the program. Like this:
$ echo now you see me # now you do not see me now you see me
This # symbol is meant for comments to other programmers (or to yourself) rather than commands to the computer. Hereâs another example, one thatâs more in that vein.
$ echo A mess of pottage # Here, I wanted the computer to write "A mess of pottage" A mess of pottage
In a shell comment, you can use the weird characters without problem. The shell knows youâre just making a comment and doesnât try to understand.
Be careful though: symbols mean different things in different contexts. The # is also often used as a prompt for the administrator. It replaces the friendly $ or %-sign to alert you that it has the power to, for example, erase anything on the computer. Thatâs why itâs important to learn to recognize your prompt. Think of the shell as your eyes and ears in the computer world, and the prompt as the computer asking for your input.
ellen% echo eight days a week # Say âeight days a weekâ eight days a week ellen% # even when I'm not commanding anything, I can make a comment
...later, if I switch to the administrator account:
ellen# â the prompt has changed from % to #~\\
~ellen# exit âbetter just exit for now
These two uses of the old # are completely unrelated (and itâs only a coincidence that the two different things both show as a #). Itâs strange... the comment is so safe, it means âdonât worry, this is only a side remark for other humans, the computer wonât mess anything up since itâll just throw this part of the text awayâ while the administrator user prompt # means âPlease be careful, remember that you have great powerâ.
We talked earlier about how which sometimes showed us programs that were placed in directories, and sometimes showed us commands that were built-in to the shell. There is a third type of command that the shell can use, and thatâs âfunctionsâ.
Iâll show you a simple one, to start with. This is on zsh:
ellen% vegetable () { echo I like to eat aubergines }
Weird, right? Iâll go through it row by row. Letâs say I wanted to often have the computer write to me how much it likes to eat aubergines. I could easily ask it to say that, like this:
ellen% echo I like to eat aubergines I like to eat aubergines
But what if I wanted to do it with just a simple command? Like this:
ellen% vegetable I like to eat aubergines
So the first line was vegetable () {. Now, vegetable is just the name of the new command. I couldâve used any word (hopefully not a word that the computer already expects to have meaning, but I can check that with which if I am unsure). The parentheses is how we let the shell know that weâre teaching it a new function. And the curly braces, these: {}, well, they surround the list of commands that the shell should do everytime the new function is called. In this case, all Iâve asked it to do is to echo something back at me.
When using <code>zsh</code>âs built-in version of which, it even knows about it:
ellen% which vegetable vegetable () { echo I like to eat aubergines }
And I can call it how many times I want:
ellen% vegetable I like to eat aubergines ellen% vegetable I like to eat aubergines ellen% vegetable I like to eat aubergines
This makes me happy. The computer now likes to eat aubergines. Teaching the shell new functions like this is great, because it can do many things at once.
Letâs say I wanted it to both praise eating potatoes and to tell me how it knows about echo:
ellen% potatowhich () { echo I like some potatoes which echo } ellen% potatowhich I like some potatoes echo: shell built-in command
The shell treats our homemade functions no different than the programs it finds in files in places like /usr/bin, and no different than the built-in shell commands either.
Before you get carried away and write hundreds of nifty functions, remember that just defining the functions at the prompt like this does not save them. Be sure to save them in a text file.