Shell Traps
Sharp shell programmers should take note of the following:
Variables are prefixed with
$,@, or%on the left side of the assignment as well as the right. A shellish assignment like:camel="dromedary"; # WRONG
won’t be parsed the way you expect. You need:
$camel="dromedary"; # ok
The loop variable of a
foreachalso requires a$. Although csh likes:foreach hump (one two) stuff_it $hump endIn Perl, this is written as:
foreach $hump ("one", "two") { stuff_it($hump); }The backtick operator does variable interpolation without regard to the presence of single quotes in the command.
The backtick operator does no translation of the return value. In Perl, you have to trim the newline explicitly, like this:
chomp($thishost = `hostname`);
Shells (especially csh) do several levels of substitution on each command line. Perl does interpolation only within certain constructs such as double quotes, backticks, angle brackets, and search patterns.
Shells tend to interpret scripts a little bit at a time. Perl compiles the entire program before executing it (except for
BEGINblocks, which execute before the compilation is done).Program arguments are available via
@ARGV, not$1,$2, and so on.The environment is not automatically made available as individual scalar variables. Use the standard
Envmodule if you want that to happen.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access