Features Bash (Unix shell)




1 features

1.1 brace expansion
1.2 startup scripts

1.2.1 execution order of startup files

1.2.1.1 when started interactive login shell
1.2.1.2 when login shell exits
1.2.1.3 when started interactive shell (but not login shell)


1.2.2 comparison bourne shell , csh startup sequences

1.2.2.1 setting inheritable environment variables
1.2.2.2 aliases , functions
1.2.2.3 commands performed @ login , logout
1.2.2.4 legacy-compatible bash startup example


1.2.3 operating system issues in bash startup


1.3 portability
1.4 keyboard shortcuts
1.5 process management
1.6 conditional execution
1.7 bug reporting





features

the bash command syntax superset of bourne shell command syntax. bash can execute vast majority of bourne shell scripts without modification, exception of bourne shell scripts stumbling fringe syntax behavior interpreted differently in bash or attempting run system command matching newer bash builtin, etc. bash command syntax includes ideas drawn korn shell (ksh) , c shell (csh) such command line editing, command history, directory stack, $random , $ppid variables, , posix command substitution syntax $(…).


when user presses tab key within interactive command-shell, bash automatically uses command line completion match partly typed program names, filenames , variable names. bash command-line completion system flexible , customizable, , packaged functions complete arguments , filenames specific programs , tasks.


bash s syntax has many extensions lacking in bourne shell. bash can perform integer calculations ( arithmetic evaluation ) without spawning external processes. uses ((…)) command , $((…)) variable syntax purpose. syntax simplifies i/o redirection. example, can redirect standard output (stdout) , standard error (stderr) @ same time using &> operator. simpler type bourne shell equivalent command > file 2>&1 . bash supports process substitution using <(command) , >(command)syntax, substitutes output of (or input to) command filename used. (this implemented through /proc/fd/ unnamed pipes on systems support that, or via temporary named pipes necessary).


when using function keyword, bash function declarations not compatible bourne/korn/posix scripts (the korn shell has same problem when using function ), bash accepts same function declaration syntax bourne , korn shells, , posix-conformant. because of these , other differences, bash shell scripts runnable under bourne or korn shell interpreters unless deliberately written compatibility in mind, becoming less common linux becomes more widespread. in posix mode, bash conforms posix more closely.


bash supports here documents. since version 2.05b bash can redirect standard input (stdin) here string using <<< operator.


bash 3.0 supports in-process regular expression matching using syntax reminiscent of perl.


bash 4.0 introduced support associative arrays. associative arrays allow fake support multi-dimensional (indexed) arrays, in similar way awk:



brace expansion

brace expansion, called alternation, feature copied c shell. generates set of alternative combinations. generated results need not exist files. results of each expanded string not sorted , left right order preserved:



users should not use brace expansions in portable shell scripts, because bourne shell not produce same output.



when brace expansion combined wildcards, braces expanded first, , resulting wildcards substituted normally. hence, listing of jpeg , png images in current directory obtained using:



in addition alternation, brace expansion can used sequential ranges between 2 integers or characters separated double dots. newer versions of bash allow third integer specify increment.



when brace expansion combined variable expansion variable expansion performed after brace expansion, in cases may necessitate use of eval built-in, thus:



startup scripts

when bash starts, executes commands in variety of dot files. though similar bash shell script commands, have execute permission enabled , interpreter directive #!/bin/bash, initialization files used bash require neither.


execution order of startup files

comparison bourne shell , csh startup sequences

elements of bash derive bourne shell , csh. these allow limited startup file sharing bourne shell , provide startup features familiar csh users.



aliases , functions

these 2 facilities, aliases csh , more general functions largely supersede them bourne shell, not typically inheritable login shell, , had redefined in each subshell spawned login shell. although there env environment variable applied problem, both csh , bash support per-subshell startup files address directly. in bash, ~/.bashrc called interactive subshells. if user-defined functions ~/.bashrc desired in login shell well, ~/.bash_login can include line below after setting of environment variables:



commands performed @ login , logout

the csh supports ~/.login file purposes of tasks performed during initial login, such displaying system load, disk status, whether email has come in, logging login time, etc. bourne shell can emulate in ~/.profile, doesn t predefine file name. achieve similar semantics csh model, ~/.bash_profile can contain line below, after environment setup , function setup:



likewise, csh has ~/.logout file run when login shell exits. bash equivalent ~/.bash_logout, , requires no special setup. in bourne shell, trap built-in can used achieve similar effect.



legacy-compatible bash startup example

the skeleton ~/.bash_profile below compatible bourne shell , gives semantics similar csh ~/.bashrc , ~/.bash_login. [ -r filename ] tests see if filename exists , readable, skipping part after && if s not.



operating system issues in bash startup

some versions of unix , linux contain bash system startup scripts, under /etc directories. bash calls these part of standard initialization, other startup files can read them in different order documented bash startup sequence. default content of root user s files may have issues, skeleton files system provides new user accounts upon setup. startup scripts launch x window system may surprising things user s bash startup scripts in attempt set user-environment variables before launching window manager. these issues can addressed using ~/.xsession or ~/.xprofile file read ~/.profile — provides environment variables bash shell windows spawned window manager need, such xterm or gnome terminal.


portability

invoking bash --posix option or stating set -o posix in script causes bash conform closely posix 1003.2 standard. bash shell scripts intended portability should @ least take account bourne shell intends replace. bash has features traditional bourne shell lacks. include:



certain extended invocation options
command substitution using $( ) notation (this feature part of posix 1003.2 standard though)
brace expansion
certain array operations, , associative arrays
the double brackets extended test construct
the double-parentheses arithmetic-evaluation construct
certain string-manipulation operations
process substitution
a regular expression matching operator
bash-specific builtins
coprocesses

a bashism portion of bash code not run on other unix shells.


keyboard shortcuts

bash uses readline provide keyboard shortcuts command line editing using default (emacs) key bindings. vi-bindings can enabled running set -o vi.


process management

the bash shell has 2 modes of execution commands: batch, , concurrent mode.


to execute commands in batch (i.e., in sequence) must separated character ; , or on separate lines:



in example, when command1 finished, command2 executed.


to have concurrent execution of command1 , command2, must executed in bash shell in following way:



in case command1 executed in background (symbol &), returning control shell executes command2 in foreground.


besides foreground , background, process can stopped. achieved typing ctrl+z while process running in foreground. list of processes, both in background , stopped, can achieved running jobs:



in output, number in brackets refers job id. plus sign signifies default process bg , fg. text running , stopped refer process state. last string command started process.


the state of process can changed using various commands. fg command brings process foreground, while bg sets stopped process running in background. bg , fg can take job id first argument, specify process act on. without one, use default process, identified plus sign in output of jobs. kill command can used end process prematurely, sending unix signal. job id must specified after percent sign:



conditional execution

bash supplies conditional execution command separators make execution of command contingent on exit code set precedent command. example:



where ./do_something executed if cd (change directory) command successful (returned exit status of zero) , echo command executed if either cd or ./do_something command return error (non-zero exit status).


for commands exit status stored in special variable $?. bash supports if ...;then ...;else ...;fi , case $variable in $pattern)...;;$other_pattern)...;; esac forms of conditional command evaluation.


bug reporting

an external command called bashbug reports bash shell bugs. when command invoked, brings user s default editor form fill in. form mailed bash maintainers (or optionally other email addresses).








Comments

Popular posts from this blog

Discography Ole Paus

Gaeta class Lerici-class minehunter

Driver.27s licenses used for identification purposes Driver's license