On Thu, 2003-08-14 at 15:00, dfroehling wrote:
Thomas Moritz schrieb:
Ich habe mir im Laufe der letzten Jahre verschiedene Sachen ausgearbeitet und als Muster abgelegt. Das ersparte mir bisher viel Sucherei. Jetzt kann ich meine Beispiele wieder etwas erweitern:-) Danke! Du zeigst mir oben, wie ich ohne Backticks, also ohne weitere Subshell, rechnen kann. Nach solch einer Variante hatte ich bisher leider nie gesucht - ging ja auch immer nach meinem Muster:-)
Wie koennte ich nun auch folgendes besser Loesen?: (vielleicht geht´s ja auch ohne nach cut zu pipen?)
read line myvar=`echo "$line" | cut -c 4-11`
myvar=$(echo "$line" | cut -c 4-11)
Worin liegt der Vorteil? Ich darf mal IEEE Std 1003.1-2001 (SUSV3) zitieren: .. Command substitution allows the output of a command to be substituted in place of the command name itself. Command substitution shall occur when the command is enclosed as follows: $(command) or (backquoted version): `command` The shell shall expand the command substitution by executing command in a subshell environment (see Shell Execution Environment ) and replacing the command substitution (the text of command plus the enclosing "$()" or backquotes) with the standard output of the command, removing sequences of one or more <newline>s at the end of the substitution. .. => Ich sehe keinen grundsätzlichen Vorteil. Beide werden von POSIX unterstützt. (Der eigentliche Vorteil von $() gegenüber `` ist die Schachtelbarkeit von $()). Aber, alte Shells (z.B. unter SunOS4) und selbst einige "moderne" Shells (Solaris-5.8, Irix-6.5; vgl. info autoconf -> Portable Shell -> Shell Substitutions)) kennen nur die ``-Variante. D.h. willst Du "nur" POSIX unterstützen, ist es prinzipiell egal, was verwendet wird. Willst Du Portabilität, nimm die Backtick-Variante. BTW, ähnliches gilt für die "Arithmetic Expressions". POSIX kennt $(()), portabel (auch auf alte Systeme) ist `expr <expression>`. Mit anderen Worten: Willst Du ein Bash-Script schreiben, nimm $() und $(()), willst Du ein zu älteren Bourne-Shells kompatibles Script schreiben, nimmt `` und `expr `. Ralf