Code Monkey home page Code Monkey logo

bash-reference's Introduction

bash-reference

Command References

sed

Bash Quick Reference


Shebang

Shebang is the '#!' in the beginning of the script.
It is used to choose the interpreter that the script will use.
If no shebang is specified, the current shell will be used.

#! /bin/bash
echo "This script is using the bash interpreter."

Variables

To access variable's data use the $ operator.
The {} is optional.

#! /bin/bash

MY_VAR="Hello World"
echo $MY_VAR
echo ${MY_VAR}ddd   # Note, this is example when to use {}

By default, all variables are global.
The local key word can be used to make function variables local.


If Else statements

#! /bin/bash

MY_VAR="var"

if [ "$MY_VAR" = "var" ]
then
    echo "This is true"
elif [ "$MY_VAR" = "Silly value" ]
then
    echo "This will not be printed"
else
    echo "This will not be printed"
fi

There are few test operators that bash provides.
For example, the -f checks if file exists.

#! /bin/bash

if [ -f $0 ]
then
    echo "File $0 exists"
fi

Loops

#! /bin/bash

FILES=$(ls .)

for FILE_NAME in ${FILES}
do
    echo $FILE_NAME
done

Positional Parameters

Each command line param can be accessed with the $ operator:

#! /bin/bash

SCRIPT_COMMAND=$0

echo "This is \$0: $SCRIPT_COMMAND "
echo "This is \$1: $1"
echo "This is \$2: $2"

The $@ variable contains list of all command line params (except for $0):

#! /bin/bash

for PARAM in $@
do
    echo "This is param value: [ $PARAM ]"
done

Exit Code

#! /bin/bash

echo "The exit code of previous command is $?"

The following script check is host is reachable:

#! /bin/bash

HOST="google.com"

ping -c 1 $HOST

if [ "$?" -eq "0" ]
then
  echo "$HOST reachable."
else
  echo "$HOST unreachable."
fi

Functions

Note that return can be used to return value from function.
If no return is used, the return value is the return value of the
last command in the function.

#! /bin/bash

#Declare hello_world
function hello_world() {
    echo "Hello World"
    return 0
}

# Call hello_world
hello_world     

Functions can access their parameters via $1, $2, etc.
Note that $0 is still the script name.

#! /bin/bash

function hello_world_with_params() {
    echo "Hello World $1 $2 $3"
}

hello_world_with_params hi hey wow 

Local variables can be declared using the local keyword.

#! /bin/bash

function local_var_func() {
    local MY_VAR="Hey there"
}

local_var_func

Wildcards

* - Matches zero or more characters.
? - Matches exactly one character.
[] - Match one character from the characters in [].
[!] - Match any character that is not in [!].
[a-z] - Match character that is in the range of characters.

bash-reference's People

Contributors

konstantin89 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.