Prev (Variables II) | Next (Functions) |
echo
, which
, and test
are commonly builtin), but many useful commands are actually Unix utilities,
such as tr
, grep
, expr
and cut
.
The backtick (`)is also often associated with external commands.
Because of this, we will discuss the backtick first.
The backtick is used to indicate that the enclosed text is to be executed
as a command. This is quite simple to understand. First, use an
interactive shell to read your full name from /etc/passwd
:
$ grep "^${USER}:" /etc/passwd | cut -d: -f5 Steve ParkerNow we will grab this output into a variable which we can manipulate more easily:
$ MYNAME=`grep "^${USER}:" /etc/passwd | cut -d: -f5` $ echo $MYNAME Steve ParkerSo we see that the backtick simply catches the standard output from any command or set of commands we choose to run. It can also improve performance if you want to run a slow command or set of commands and parse various bits of its output:
#!/bin/sh find / -name "*.html" -print | grep "/index.html$" find / -name "*.html" -print | grep "/contents.html$"
#!/bin/sh HTML_FILES=`find / -name "*.html" -print` echo $HTML_FILES | grep "/index.html$" echo $HTML_FILES | grep "/contents.html$"
find
once, roughly
halving the execution time of the script.
We discuss specific examples further in the
Hints and Tips section of this tutorial.
Prev (Variables II) | Next (Functions) |