Bash shell scripting

Bash – add a number to a variable

To add a number to a variable in bash, there are many approaches. Some of these are: Declare variable as integer Once a variable is read more

Bash – append text to a variable

Variable in string approach Use curly braces {} around variable (${VARNAME}) in case text begins with alphanumeric character. Concatenate variable with string Use =+ operator read more

Bash – how to check if a variable is set

To check if a variable is set or empty, we can use couple of approaches. One approach is to use empty string check (if [ read more

Bash – how to compare file timestamps

Sometimes we need to perform some action if a file is newer than other file. This can be used to recreate generated file if source read more

Bash – how to find last command exit status code

When writing bash scripts or running something on command line, last command exit code status is stored in environment variable $?. It can also be read more

Bash – how to get main program and current file dir location

Get main program directory location In case you have a program main.sh in /opt/tmp which includes utils.sh from /opt/tmp/dir1, then this code will return /opt/tmp read more

Bash – how to redirect stderr to stdout or file

On Linux or Unix, error from a command (stderr) can be redirected to stdout or a file. These are the file descriptor id used on read more

Bash – how to run custom commands at script exit

Bash scripts can trap EXIT signal and run clean up commands before exit. This is useful if a user kills a script or the script read more

Bash – how to stop at error

To stop a bash script at first error use set -e. To undo it use set +e.

Bash – how to use functions – quick tutorial

Using functions in bash is fairly useful to organise and reuse code. Here is quick beginner tutorial with examples on using bash functions. Declaring functions read more

Bash – iterate over array

These are few ways of iterating on bash arrays to read and or modify array’s elements in bash on Linux or Mac. Here are some read more

Bash – local and global variables

Bash functions can have local variables. This can protect the accidental modification of global variables when function is called inline within same shell. Here are read more

Bash – newline and other escape character in string

To have newline (\n) and other escape characters in bash strings, we can use $’string’ syntax (ANSI-C Quoting). $’string’ expands to string, with backslash-escaped characters read more

Bash – pass all arguments from one script to another

When writing a wrapper bash script, we often need to pass all arguments passed to the wrapper scrip to another script. Here is quick bash read more

Bash – set default value if a variable is empty

There are multiple ways to check if a variable is empty and then set it to some default value. Shell parameter expansion gives us an read more

Bash – variables in double quotes vs without quotes

Bash variables without double quotes can give surprising results. The special characters inside a variable when not double quoted, gets interpreted by bash. Few points read more

Bash associative array tutorial

Bash associative arrays are supported in bash version 4. Here is a quick start tutorial for using bash associative arrays. Declare and initialize associative array read more

Bash check if file begins with a string

Bash code to check if file begins with a string Note the presence of double square brackets ([[ ]]) Bash code to check if file read more

Bash shell – check if file or directory exists

Bash code snippet to check if file exists FILE=/etc/passwd if [ -f $FILE ] ; then echo “$FILE exists and is regular file” else echo read more

Can global variables be modified in bash function?

Global variables can be accessed in bash function without declaring anything and when these are modified the behaviour depend on whether function is called in read more

Find memcache request hit rate on linux command line

Memcached gives you stats like total hits so far and total miss. It can also tell you total uptime. This can be used to find read more

How to return a value from bash function

Returning a variable from functions in bash script can be little tricky. Bash functions are not similar to functions in other languages but these are read more

Iterate over specific file extension in a dir in shell script

Sometimes we need to iterate over all files of a specific file extension using a bash script. This can be used to convert one type read more

Linux – Yesterday’s Date in YYYYMMDD format

Linux Bash code for yesterday’s date in YYYYMMDD format $ date –date=”yesterday” +”%Y%m%d” 20151128 $ date –date=”-1 days” +”%Y%m%d” 20151128 Mac Bash code for yesterday’s read more

bash – extract urls from xml sitemap

Command to get urls from a sitemap url: To get these in a bash variable for iteration, use sub-shell command execution as shown below: Use read more

bash – how to use regex in if condition

Bash built in double square brackets can be used for regex match in if condition. This can be pretty powerful and can be used in read more