Bash

From S23Wiki
Jump to: navigation, search

The great GNU Bourne-Again Shell

Bash is the shell, or command language interpreter, that will appear in the GNU Operating System.

Bash is an sh-compatible shell that incorporates useful features from the Korn shell (ksh) and C shell (csh).

Bash Links

Useful Keyboard combinations in Bash

With control (crtl)

  • crtl-a = start of line
  • crtl-e = end of line
  • crtl-l = clears the screen, like command clear
  • crtl-p = previous command from history, like page up key
  • crtl-r = reverse search in history(incremental)

With escape(esc)

  • esc-f = one word forward
  • esc-b = one word backward

Operators

Assignment Operators

=

All-purpose assignment operator, which works for both arithmetic and string assignments.Do not confuse the "=" assignment operator with the = test operator.

Arithmetic Operators

+

plus

-

minus

*

multiplication

/

division

**

exponentiation

%

modulo, or mod (returns the remainder of an integer division operation)

+=

"plus-equal" (increment variable by a constant). let "var += 5" results in var being incremented by 5.

-=

"minus-equal" (decrement variable by a constant)

*=

"times-equal" (multiply variable by a constant). let "var *= 4" results in var being multiplied by 4.

/=

"slash-equal" (divide variable by a constant)

%=

"mod-equal" (remainder of dividing variable by a constant)

Logical Operators

&&

and (logical). if [ $condition1 ] && [ $condition2 ]

||

or (logical). if [ $condition1 ] || [ $condition2 ]

Dont confuse those with #Conditional executing.

Comparison Operators

Integers
-eq

is equal to. if [ "$a" -eq "$b" ]

-ne

is not equal to. if [ "$a" -ne "$b" ]

-gt

is greater than. if [ "$a" -gt "$b" ]

-ge

is greater than or equal to if. [ "$a" -ge "$b" ]

-lt

is less than. if [ "$a" -lt "$b" ]

-le

is less than or equal to. if [ "$a" -le "$b" ]

<

is less than. (within double parentheses) (("$a" < "$b"))

<=

is less than or equal to. (within double parentheses). (("$a" <= "$b"))

>

is greater than (within double parentheses). (("$a" > "$b"))

>=

is greater than or equal to (within double parentheses). (("$a" >= "$b"))


Strings
=

is equal to. if [ "$a" = "$b" ]

==

is equal to. if [ "$a" == "$b" ]

!=

is not equal to. if [ "$a" != "$b" ]

<

is less than, in ASCII alphabetical order

>

is greater than, in ASCII alphabetical order

-z

string is "null", that is, has zero length

-n

string is not "null".

Bitwise Operators

<<

bitwise left shift (multiplies by 2 for each shift position)

<<=

"left-shift-equal" let "var <<= 2" results in var left-shifted 2 bits (multiplied by 4)

>>

bitwise right shift (divides by 2 for each shift position)

>>=

"right-shift-equal" (inverse of <<=)

&

bitwise and

&=

bitwise and-equal

|

bitwise OR

|=

bitwise OR-equal

~

bitwise negate

!

bitwise NOT

^

bitwise XOR

^=

bitwise XOR-equal


Misc Operators

,

comma operator . The comma operator chains together two or more arithmetic operations. All the operations are evaluated (with possible side effects), but only the last operation is returned.

Conditional executing

command1 && command2

command2 is executed if, and only if, command1 returns an exit status of zero. (exit status zero means succesful)

command1 || command2

command2 is executed if and only if command1 returns a non-zero exit status. (non-zero exit status means failed)

command1 && command2 || command3 

if command1 is executed successfully then shell will run command2 and if command1 is not successful then command3 is executed.

Wildcards / regex

These can be used on filenames.

*

zero or more characters

?

exactly one character

[abcde]

exactly one character listed

[a-e]

exactly one character in the given range

[!abcde]

any character that is not listed

[!a-e]

any character that is not in the given range

{debian,linux}

exactly one entire word in the options given

You can use wildcards with any command that accepts file names as arguments.

also see: Bash Scripts

<man>bash</man>

Bash built-in commands

GNU bash, version 2.05a.0(1)-release (i386-pc-linux-gnu)
These shell commands are defined internally.  Type `help' to see this list.
Type 'help name' to find out more about the function `name'.
Use 'info bash' to find out more about the shell in general.

A star (*) next to a name means that the command is disabled.

 %[DIGITS | WORD] [&]               . filename
 :                                  [ arg... ]
 alias [-p] [name[=value] ... ]     bg [job_spec]
 bind [-lpvsPVS] [-m keymap] [-f fi break [n]
 builtin [shell-builtin [arg ...]]  case WORD in [PATTERN [| PATTERN].
 cd [-PL] [dir]                     command [-pVv] command [arg ...]
 compgen [-abcdefgjkvu] [-o option] complete [-abcdefgjkvu] [-pr] [-o 
 continue [n]                       declare [-afFrxi] [-p] name[=value
 dirs [-clpv] [+N] [-N]             disown [-h] [-ar] [jobspec ...]
 echo [-neE] [arg ...]              enable [-pnds] [-a] [-f filename] 
 eval [arg ...]                     exec [-cl] [-a name] file [redirec
 exit [n]                           export [-nf] [name ...] or export 
 false                              fc [-e ename] [-nlr] [first] [last
 fg [job_spec]                      for NAME [in WORDS ... ;] do COMMA
 function NAME { COMMANDS ; } or NA getopts optstring name [arg]
 hash [-r] [-p pathname] [-t] [name help [-s] [pattern ...]
 history [-c] [-d offset] [n] or hi if COMMANDS; then COMMANDS; [ elif
 jobs [-lnprs] [jobspec ...] or job kill [-s sigspec | -n signum | -si
 let arg [arg ...]                  local name[=value] ...
 logout                             popd [+N | -N] [-n]
 printf format [arguments]          pushd [dir | +N | -N] [-n]
 pwd [-PL]                          read [-ers] [-t timeout] [-p promp
 readonly [-anf] [name ...] or read return [n]
 select NAME [in WORDS ... ;] do CO set [--abefhkmnptuvxBCHP] [-o opti
 shift [n]                          shopt [-pqsu] [-o long-option] opt
 source filename                    suspend [-f]
 test [expr]                        time [-p] PIPELINE
 times                              trap [arg] [signal_spec ...] or tr
 true                               type [-apt] name [name ...]
 typeset [-afFrxi] [-p] name[=value ulimit [-SHacdflmnpstuv] [limit]
 umask [-p] [-S] [mode]             unalias [-a] [name ...]
 unset [-f] [-v] [name ...]         until COMMANDS; do COMMANDS; done
 variables - Some variable names an wait [n]
 while COMMANDS; do COMMANDS; done  { COMMANDS ;

use help <command>, f.e help shift to get more information about the commands.

Bash on Mac OS X

Bash on Windows