bash


Bash: Get Filename, File Extension and Path from Full Path

The following commands will allow you extract various information from the full path of a file.

Part of the information is the filename, the file extension, the file base and the directory it is located in.

# Truncate the longest match of */ from the beginning of the string
filename="${fullpath##*/}";
# Get the sub-string from the start (position 0) to the position where the filename starts
directory="${fullpath:0:${#fullpath} - ${#filename}}";
# Strip shortest match of . plus at least one non-dot char from end of the filename
base="${filename%.[^.]*}";
# Get the sub-string from length of base to end of filename
extension="${filename:${#base} + 1}";
# If we have an extension and no base, it means we do not really have an extension but only a base
if [[ -z "$base" && -n "$extension" ]]; then
  base=".$extension";
  extension="";
fi
echo -e "Original:\t'$fullpath':\n\tdirectory:\t'$directory'\n\tfilename:\t'$filename'\n\tbase name:\t'$base'\n\textension:\t'$extension'"

Bash: Get random number that belongs in a range 1

In case you need to create a random number that is between a certain range, you can do it in a very easy way in bash.

Bash has the $RANDOM internal function that returns a different (pseudo) random integer at each invocation. The nominal range of the results is 0 – 32767 (it is the range of a signed 16-bit integer).

We use the result of $RANDOM to create our number using the following script:

FLOOR=10;
CEILING=100;
RANGE=$(($CEILING-$FLOOR+1));
echo "You will generate a random number between $FLOOR and $CEILING (both inclusive). There are $RANGE possible numbers!"
RESULT=$RANDOM;
echo "We just generated the random number $RESULT, which might not be in the range we want";
let "RESULT %= $RANGE";
RESULT=$(($RESULT+$FLOOR));

echo "Congratulations! You just generated a random number ($RESULT) the is in between $FLOOR and $CEILING (inclusive)";

We use basic math operations on the variables to create the random number that is in the range we like without need-less loops.

What we do is:

  1. We compute the length of the range of valid numbers (ceiling-floor)
    Here we added the +1 because we want the ceiling to be included in the result set.
  2. We generate a random number that is between 0 and the length of range using modulo to limit the results.
  3. We add to the result the value of the floor and this create our final result which is a number equal or greater to the floor and equal or smaller to the ceiling

Remove the last character from a bash variable

The following method, removes the last character from a bash variable by using a regular expression that matches any character.


VAR=${VAR%?};

The functionality ${string%substring} deletes shortest match of $substring from the back of $string.
The question mark ? matches zero or one of the previous regular expression. It is generally used for matching single characters.
So by using ? as our $substring we instruct bash to match any character at the end of the variable and remove it.

Example


$ VAR="Banana";
$ VAR=${VAR%?};
$ echo $VAR;
Banan


Remove the first character from a bash variable

The following method, removes the first character from a bash variable by using a regular expression that matches any character.


VAR=${VAR#?};

The functionality ${string#substring} deletes shortest match of $substring from the front of $string.
The question mark ? matches zero or one of the previous regular expression. It is generally used for matching single characters.
So by using ? as our $substring we instruct bash to match any character at the beginning of the variable and remove it.

Example


$ VAR="Banana";
$ VAR=${VAR#?};
$ echo $VAR;
anana