Linux Command Line & Bash Shell Shortcuts

Introduction

Although you may think that you have learned to master Linux command line with bash shell, there are always some new tricks to learn to make your command line skills more efficient. This article will teach you a few more basic tricks on how to make your life with the Linux command line & bash more bearable and even enjoyable.

Bash Command History Expansion

This section will mostly deal with bash shortcuts in combination with three bash history expansion characters “!”, “^” and “#”. Bash Command History Expansion character “!” indicates start of history expansion. The “^” is a substitution character to modify a previously run command. The last optional character is “#”, which denotes the reminder of the line as a comment.

Repeat last command

$ echo Bash Shortcuts
Bash Shortcuts
$ !!
echo Bash Shortcuts
Bash Shortcuts

!! is probably the easiest and most popular bash shortcut, which simply shows and executes your last entered command.

Read more

How to count occurrence of a specific character in a string or file using bash

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/\(.\)/\1\n/g' | grep l | wc -l
2

Read more

BASH Scripting: Parenthesis Explained

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++))
((var = 3))
for ((i = 0; i < VAL; i++))
echo $((var + 2))

Read more

How to test for null or empty variables within Bash script

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

#!/bin/bash 

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


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


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


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


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

Read more

SyntaxError: Non-ASCII character – Python with UTF-8 encoding

Question

:
My Python program produce a following error message upon execution:

 SyntaxError: Non-ASCII character '\xc4' in file test.py on line 1, but no encoding declared; 

Answer:

Normally the above error message is displayed by python when other characters other then ASCII are used withing your code. The solution is to either remove all non-ASCII characters or include the bellow line into your code to enable UTF-8 encoding:

# - *- coding: utf- 8 - *-

Read more

How to obtain an user input with Python raw_input function example

The Python raw_input() function is used to read a string from standard input such as keyboard. This way a programmer is able to include user inserted data into a program. Let’s start with a simple example using python script to ask for an user name.

print "What is your name?"
name = raw_input()
print "Hello %s!" % name

First, we print string What is your name? telling the user what we expect him to input. Next, using the raw_input() function the standard input is assigned to a variable name. Lastly, we print the value of variable name to standard output.

$ python input.py 
What is your name?
Monty Python
Hello Monty Python!

Read more

Check your GMAIL inbox for new emails with Bash script

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. check_email.sh

#!/bin/bash

username="USERNAME"
password="PASSWORD"
echo
curl -u $username:$password --silent "https://mail.google.com/mail/feed/atom" |  grep -oPm1 "(?<=<title>)[^<]+" | sed '1d'

Read more

How to access and print command line arguments with Python

The following is an example on how to pass and access command line arguments which a Python script. Save the following python script to a file eg. python-arguments.py

from sys import argv

name, first, second, third, fourth = argv

print "Script name is:", name
print "Your first argument is:", first
print "Your second argument is:", second
print "Your third argument is:", third
print "Your fourth argument is:", fourth


# Alternatively we can access "argv" argument list directly using range. For exmaple:

# Print all arguments except script name
print argv[1:]

# Print second argument
print argv[2]

# Print second and third argument
print argv[2:4]

# Print last argument
print argv[-1]

Read more

Extract all URLs using Beautiful Soup and Python3

The following link will extract all URL’s for a given web page.

#!/usr/bin/env python3

# Python Version:  3.4.2
# bs4 version: 4.3.2-2

from urllib.request import urlopen
from bs4 import BeautifulSoup

html = urlopen("http://gnu.org") # Insert your URL to extract
bsObj = BeautifulSoup(html.read());

for link in bsObj.find_all('a'):
    print(link.get('href'))

Read more

How to fill all empty valued cells within a CSV file with sed and bash shell

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.
Example:

$ cat test 
1       2       4       4
2       3       3       3
$ sed 's/\t/,/g' test 
1,2,4,4
2,3,3,3
OR
$ cat test | tr '\t' ','
1,2,4,4
2,3,3,3

Read more

Writing a C style bash for loop – example

If you are stubborn C programmer and wish to get your way when using BASH you will be happy to know that BASH offers C style syntax for writing for loops. Below you can find two examples of C style bash for loop:

Simple c-style bash for look with three iterations:

#!/bin/bash
MAX=3
for ((i=1; i <= MAX ; i++)) ; do
	echo "$i"
done

Read more