Bash getting the parent folder name from a bash defined variable

Problem

You have defined a variable in a bash script (ie MY_FOLDER) but you also want to dynamically get the parent folder (ie MY_FOLDER can have multiple values).

Solution

Use the dirname as in the following

MY_FOLDER=/home/user/first_folder/second_folder
MY_PARENT_FOLDER=$(dirname $MY_FOLDER)

echo $MY_FOLDER
echo $MY_PARENT_FOLDER

.......
/home/user/first_folder/second_folder
/home/user/first_folder

Removing a range of history lines from bash history

Problem

You would like to remove some lines from the bash history (ie using cut and paste with wrong values).

Solution

You can use the following, taken from the answer here

for i in {1..no_of_lines}; do history -d start_line; done

where start_line is the start of the lines you want to remove and no_of_lines is the number of lines you want to be removed.

Bash script using default value for empty parameter

Problem

You have a bash script that accepts parameter(s) but you want to give a default value for a missing parameter.

Solution

For example you have a script that accepts a parameter with the year and month (ie 201703), but you want to give it a default of two months ago if the parameter is missing.

#!/bin/bash

LM=$(date -d '2 month ago' +%Y%m)
YEARMONTH=${1:-${LM}}
echo $YEARMONTH