The following bash script example we show some of the way how to check for an empty or null variable using bash:

if [ -z "$1" ]; then
    echo "Empty Variable 1"

if [ -n "$1" ]; then
    echo "Not Empty Variable 2"

if [ ! "$1" ]; then
    echo "Empty Variable 3"

if [ "$1" ]; then
    echo "Not Empty Variable 4"

[[ -z "$1" ]] && echo "Empty Variable 5" || echo "Not empty Variable 5"

Author: Tobin Harding
Here we briefly outline some of the major use cases for brackets, parenthesis, and braces in BASH scripting, see bottom of page for definition of these three terms. Double parentheses (( )) are used for arithmetic:
((var = 3))
for ((i = 0; i < VAL; i++))
echo $((var + 2))

Below you can find some hints on how to count an occurrence of specific character in a file or in a string. Le's say we have a string "Hello Bash":
$ STRING="Hello Bash"
$ echo $STRING
Hello Bash
Using bash shell we can now count an occurrence of any given character. For example let's count number of occurrences of a character l:
$ echo $STRING | sed -e 's/\(.\)/\n/g' | grep l | wc -l

Let's start by a simple execution example of GNU R Rscript front-end script. Use which command to locate Rscript interpreter:
$ which Rscript
alternatively define your interpreter as /usr/bin/env Rscript

print("Hello R")

The total number of supplied command-line arguments is hold by a in bash's internal variable $#. Consider a following example of simple bash script which will print out a total number of supplied command-line arguments to the STDOUT:
echo $#
Save the above into a file called eg. and execute:
$ bash 1 2 3 4

When using a bash shell all your entered commands are remembered by history library. The history library will keep track of every command you have entered. This is a default for most if not all Linux systems. However, the commands you enter are first temporarily stored into an internal memory and are written to your ~/.bash_history only after you properly exit your shell session.

Depending on your shell usage this may cause some undesired results. For example, if your connection to a remote host gets disconnected, your history file will not get updated and thus you will lose all you previously entered commands. Furthermore, while your commands for one session are temporarily stored within system's internal memory you would not be able to access it from another shell session.

Use the following linux command to force your shell to append every command entered during a current shell session into ~/.bash_history file:
shell 1: $ history -a

Commands history is a great feature of the bash shell. However, there are times when it is best to disable it. One good example when you might prefer your bash shell commands history to be disabled is on the production server accessible from the external network where potential attacker might gain an access to your server and re-read your history file to search for useful commands, services in use or accidentally inserted passwords. Below you can find bunch of commands to help you disable history from being stored or how to remove all currently stored commands.

Where are history commands stored

All commands your enter on the shell are stored within your local directory into a file called .bash_history. This is a default history file defined by HISTFILE variable:
# echo $HISTFILE


This error message appears when you try to remove, move or copy a long list of files. When using your shell a command can only accept a limited number of arguments. When the number of arguments supplied to the command exceeds the permitted number of arguments an error message will appear:
-bash: /bin/rm: Argument list too long
linux command to find your limit for maximum arguments:
# getconf ARG_MAX
# rm *
-bash: /bin/rm: Argument list too long

Below is a simple function to check for a prime number. The function is_prime_number() returns False if the number supplied is less than 2 and if the number is equally divisible with some other number different than 1 and itself. If none of the previous conditions apply the function will return True. The below python script will let user to decide how many numbers needs to be check to see whether the number is prime number:
#!/usr/bin/env python

prime_numbers = 0

def is_prime_number(x):
    if x >= 2:
        for y in range(2,x):
            if not ( x % y ):
                return False
	return False
    return True

for i in range(int(raw_input("How many numbers you wish to check: "))):
    if is_prime_number(i):
        prime_numbers += 1
        print i

print "We found " + str(prime_numbers) + " prime numbers."

The following bash script can be used to fill empty cells within a CSV file. We assume that your file is evenly distributed so that it contains same number of columns in each row separated by comma. If your file is TABseparated use a following linux command to convert it to comma separated value file before you proceed.
$ cat test 
1       2       4       4
2       3       3       3
$ sed 's/\t/,/g' test 
$ cat test | tr '\t' ','

Below you can find a short script on how to convert a binary number to decimal using python casting. Casting is when you convert variable from one type to another. In this case we use python casting to convert string to decimal number that is integer. Therefore, when you attempt to use casting to convert variable type to integer make sure that you variable type imput is a string. Create a following file with a following content:
#!/usr/bin/env python

while True:
        # Try to convert bunary to decimal
        decimal_num = int(raw_input("Enter a binary number: "), 2)
        # If we fail we ask again user to enter binary number
    except ValueError:
        print "Your input is not a binary number! Please try again."
        # Exit program if the conversion from binary to decimal was successful
# print converted decimal number
print decimal_num
Save your script as eg., make it executable and execute:
$ chmod +x
$ ./

Here a listed few of many ways how to extract number from a string. For all the examples below we will use sentence I am 999 years old. where the aim is to exctract nunber 999. Let's start by using tr command:
$ NUMBER=$(echo "I am 999 years old." | tr -dc '0-9')
$ echo $NUMBER

In case you wish to automate your things with your gmail email. Here is a simple script on how to access your gmail account with bash script. Before you run the script make sure that curl command is available on your system as this script depends on it. The below script is a great way to quickly check your gmail inbox with a single command. Open your favorite text edit and create a bash script file with some arbitrary file name eg.

curl -u $username:$password --silent "" |  grep -oPm1 "(?<=<title>)[^<]+" | sed '1d'

Submit your RESUME, create a JOB ALERT.
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
Get extra help by visiting our LINUX FORUM or simply use comments below.

You may also be interested in: