×
Create a new article
Write your page title here:
We currently have 3,189 articles on s23. Type your article name above or create one of the articles listed here!



    s23
    3,189Articles
    Revision as of 14:45, 7 April 2006 by imported>mutante

    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

    • ^a = start of line


    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

    Cookies help us deliver our services. By using our services, you agree to our use of cookies.
    Cookies help us deliver our services. By using our services, you agree to our use of cookies.