buhong 发表于 2018-8-24 10:32:31

各shell 之间的差异

不同的shell 有不同的写法,以下是各个shell的比较:  
Feature
  
C/TC
  
Bourne
  
Bash
  
Korn
  

  
Variables:
  

  
Assigning values to local variables
  
set x = 5
  
x=5
  
x=5
  
x=5
  

  
Assigning variable attributes
  

  
declare or
  

  
typeset
  

  
typeset
  

  
Assigning values to environment variables
  
setenv NAME Bob
  
NAME='Bob'; export NAME
  
export NAME='Bob'
  
export NAME='Bob'
  

  
Read-Only Variables:
  

  
Accessing variables
  

  
echo $NAME
  

  
set var = net
  

  
echo ${var}work
  

  
network
  

  
echo $NAME
  

  
var=net
  

  
echo ${var}work
  

  
network
  

  
echo $NAME
  

  
var=net
  

  
echo ${var}work
  

  
network
  

  
echo $NAME or print $NAME
  

  
var=net
  

  
print ${var}work
  

  
network
  

  
Number of characters
  
echo $%var (tcsh only)
  
N/A
  
${#var}
  
${#var}
  

  
Special Variables:
  

  
PID of the process
  
$$
  
$$
  
$$
  
$$
  

  
Exit status
  
$status, $?
  
$?
  
$?
  
$?
  

  
Last background job
  
$! (tcsh only)
  
$!
  
$!
  
$!
  

  
Arrays:
  

  
Assigning arrays
  
set x = ( a b c )
  
N/A
  

  
y='a'; y='b'; y='c'
  

  
fruit=(apples pears peaches plums)
  

  
y='a'; y='b'; y='c'
  

  
set –A fruit apples pears plums
  

  
Accessing array elements
  
echo $x $x
  
N/A
  
echo ${y} ${y}
  
print ${y} ${y}
  

  
All elements
  
echo $x or $x
[*]
  
N/A
  
echo ${y
[*]}, ${fruit}
  
print ${y
[*]}, ${fruit}
  

  
No. of elements
  
echo $#x
  
N/A
  
echo $y{#
[*]}
  
print ${#y
[*]}
  

  
Command Substitution:
  

  
Assigning output of command to variable
  
set d = `date`
  
d=`date`
  
d=$(date) or d=`date`
  
d=$(date) or d=`date`
  

  
Accessing values
  

  
echo $d
  

  
echo $d, $d,
  

  
   ...
  

  
echo $#d
  

  
echo $d
  
echo $d
  
print $d
  

  
Command Line Arguments (Positional Parameters):
  

  
Accessing
  

  
$argv, $argvor
  

  
$1, $2 ...
  

  
$1, $2 ... $9
  
$1, $2, ... ${10} ...
  
$1, $2, ... ${10} ...
  

  
Setting positional parameters
  
N/A
  

  
set ab c
  

  
set `date`
  

  
echo $1 $2 ...
  

  
set ab c
  

  
set `date` or set $(date)
  

  
echo $1 $2 ...
  

  
set abc
  

  
set `date` or set $(date)
  

  
print $1 $2 ...
  

  
No. of command line arguments
  

  
$#argv
  

  
$# (tcsh)
  

  
$#
  
$#
  
$#
  

  
No. of characters in $arg
  
$%1, $%2, (tcsh)
  
N/A
  
N/A
  
N/A
  

  
Metacharacters for Filename Expansion:
  

  
Matches for:
  

  
Single character
  
?
  
?
  
?
  
?
  

  
Zero or more characters
  
*
  
*
  
*
  
*
  

  
One character from a set
  

  

  

  

  

  
One character from a range of characters in a set
  

  

  

  

  

  
One character not in the set
  

  
N/A(csh)
  

  
[^abc](tcsh)
  

  
[!abc]
  
[!abc]
  
[!abc]
  

  
? matches zero or one occurrences of any pattern in the parentheses. The vertical bar represents an OR condition; e.g., either 2 or 9. Matches abc21, abc91, or abc1.
  
   abc?(2|9)1
  
abc?(2|9)1
  

  
Filenames not matching a pattern
  
^pattern (tcsh)
  

  
I/O Redirection and Pipes:
  

  
Command output redirected to a file
  
cmd > file
  
cmd > file
  
cmd > file
  
cmd > file
  

  
Command output redirected and appended to a file
  
cmd >> file
  
cmd >> file
  
cmd >> file
  
cmd >> file
  

  
Command input redirected from a file
  
cmd < file
  
cmd < file
  
cmd < file
  
cmd < file
  

  
Command errors redirected to a file
  
(cmd > /dev/tty)>&errors
  
cmd 2>errors
  
cmd 2> file
  
cmd 2> errors
  

  
Output and errors redirected to a file
  
cmd >& file
  
cmd > file 2>&1
  
cmd >& file or cmd &> file or cmd > file 2>&1
  
cmd > file 2>&1
  

  
Assign output and ignore noclobber
  
cmd >| file
  
N/A
  
cmd >| file
  
cmd >| file
  

  
here document
  

  
cmd
页: [1]
查看完整版本: 各shell 之间的差异